//go:build end2end // +build end2end package end2end import ( "context" "encoding/json" "fmt" "net/http" "net/http/httptest" "testing" "git.gocasts.ir/ebhomengo/niki/delivery/http_server/end2end/setup" "git.gocasts.ir/ebhomengo/niki/entity" adminserviceparam "git.gocasts.ir/ebhomengo/niki/param/admin/admin" param "git.gocasts.ir/ebhomengo/niki/param/agent/kind_box" "github.com/labstack/echo/v4" "github.com/stretchr/testify/assert" ) const ( agentPhoneNumber = "09384664403" agentPassword = "Abc123456" ) func TestAgent_KindBox_Get(t *testing.T) { kindBox := entity.KindBox{ ID: 2, SerialNumber: "serial-2", ReceiverAgentID: 4, Status: entity.KindBoxAssignedReceiverAgentStatus, } url := fmt.Sprintf("/agents/kindboxes/%d", kindBox.ID) teardown := setup.SeedMariaDB(testContainer.GetMariaDBConfig()) t.Cleanup(teardown) token, err := getToken() if err != nil { t.Fatalf("could not login: %s", err) } resRecord := createRequest(http.MethodGet, url, token) var res param.GetKindBoxResponse err = json.NewDecoder(resRecord.Body).Decode(&res) assert.NoError(t, err, "could not decode response body") assert.Equal(t, http.StatusOK, resRecord.Code) assert.Equal(t, kindBox.ID, res.ID) assert.Equal(t, kindBox.ReceiverAgentID, res.ReceiverAgentID) assert.Equal(t, kindBox.SerialNumber, res.SerialNumber) assert.Equal(t, kindBox.Status, res.Status) } func getToken() (string, error) { res, err := services.AdminSvc.LoginWithPhoneNumber( context.Background(), adminserviceparam.LoginWithPhoneNumberRequest{ PhoneNumber: agentPhoneNumber, Password: agentPassword, }, ) if err != nil { return "", err } return res.Tokens.AccessToken, nil } func createRequest(method string, url string, token string) *httptest.ResponseRecorder { req := httptest.NewRequest(method, url, nil) req.Header.Set(echo.HeaderContentType, echo.MIMEApplicationJSON) req.Header.Set(echo.HeaderAuthorization, fmt.Sprintf("Bearer %s", token)) res := httptest.NewRecorder() // Serve the request testServer.Serve(res, req) return res }