forked from ebhomengo/niki
1
0
Fork 0

test(agent-kindbox-e2e): adding e2e test for

"agents/kinboxes/return/:id" api
This commit is contained in:
mamad 2024-09-02 18:56:22 +03:30
parent d0aa6a6fe3
commit f3eeb124f9
1 changed files with 54 additions and 8 deletions

View File

@ -4,9 +4,11 @@
package end2end package end2end
import ( import (
"bytes"
"context" "context"
"encoding/json" "encoding/json"
"fmt" "fmt"
"log"
"net/http" "net/http"
"net/http/httptest" "net/http/httptest"
"testing" "testing"
@ -14,21 +16,23 @@ import (
"git.gocasts.ir/ebhomengo/niki/delivery/http_server/end2end/setup" "git.gocasts.ir/ebhomengo/niki/delivery/http_server/end2end/setup"
"git.gocasts.ir/ebhomengo/niki/entity" "git.gocasts.ir/ebhomengo/niki/entity"
adminserviceparam "git.gocasts.ir/ebhomengo/niki/param/admin/admin" adminserviceparam "git.gocasts.ir/ebhomengo/niki/param/admin/admin"
param "git.gocasts.ir/ebhomengo/niki/param/agent/kind_box" adminParam "git.gocasts.ir/ebhomengo/niki/param/admin/kind_box"
agentParam "git.gocasts.ir/ebhomengo/niki/param/agent/kind_box"
"github.com/labstack/echo/v4" "github.com/labstack/echo/v4"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
) )
const ( const (
agentPhoneNumber = "09384664403" agentPhoneNumber = "09384664403"
agentPassword = "Abc123456" agentPassword = "Abc123456"
agentID uint = 4
) )
func TestAgent_KindBox_Get(t *testing.T) { func TestAgent_KindBox_Get(t *testing.T) {
kindBox := entity.KindBox{ kindBox := entity.KindBox{
ID: 2, ID: 2,
SerialNumber: "serial-2", SerialNumber: "serial-2",
ReceiverAgentID: 4, ReceiverAgentID: agentID,
Status: entity.KindBoxAssignedReceiverAgentStatus, Status: entity.KindBoxAssignedReceiverAgentStatus,
} }
url := fmt.Sprintf("/agents/kindboxes/%d", kindBox.ID) url := fmt.Sprintf("/agents/kindboxes/%d", kindBox.ID)
@ -41,8 +45,8 @@ func TestAgent_KindBox_Get(t *testing.T) {
t.Fatalf("could not login: %s", err) t.Fatalf("could not login: %s", err)
} }
resRecord := createRequest(http.MethodGet, url, token) resRecord := createRequest(http.MethodGet, url, token, nil)
var res param.GetKindBoxResponse var res agentParam.GetKindBoxResponse
err = json.NewDecoder(resRecord.Body).Decode(&res) err = json.NewDecoder(resRecord.Body).Decode(&res)
assert.NoError(t, err, "could not decode response body") assert.NoError(t, err, "could not decode response body")
@ -53,6 +57,35 @@ func TestAgent_KindBox_Get(t *testing.T) {
assert.Equal(t, kindBox.Status, res.Status) assert.Equal(t, kindBox.Status, res.Status)
} }
// TODO: we can add more tests for return.
// we can get check if the data is updated in db as expects
// or api should return error if the kindbox is not for this agent
// CHECK: is the flow correct? agent can change kindbox status to return no matter what was the previous status?
func TestAgent_KindBox_Return(t *testing.T) {
kindBoxReqBody := agentParam.ReturnKindBoxRequest{SerialNumber: "new-serial"}
var kindBoxID uint = 2
url := fmt.Sprintf("/agents/kindboxes/return/%d", kindBoxID)
teardown := setup.SeedMariaDB(testContainer.GetMariaDBConfig())
t.Cleanup(teardown)
token, err := getToken()
if err != nil {
t.Fatalf("could not login: %s", err)
}
resRecord := createRequest(http.MethodPatch, url, token, kindBoxReqBody)
updatedKindBox, err := services.AdminKindBoxSvc.Get(
context.Background(),
adminParam.KindBoxGetRequest{KindBoxID: kindBoxID},
)
assert.Equal(t, http.StatusNoContent, resRecord.Code)
assert.Equal(t, kindBoxReqBody.SerialNumber, updatedKindBox.SerialNumber)
assert.Equal(t, agentID, updatedKindBox.ReceiverAgentID)
assert.Equal(t, entity.KindBoxReturnedStatus, updatedKindBox.Status)
}
func getToken() (string, error) { func getToken() (string, error) {
res, err := services.AdminSvc.LoginWithPhoneNumber( res, err := services.AdminSvc.LoginWithPhoneNumber(
context.Background(), context.Background(),
@ -67,8 +100,21 @@ func getToken() (string, error) {
return res.Tokens.AccessToken, nil return res.Tokens.AccessToken, nil
} }
func createRequest(method string, url string, token string) *httptest.ResponseRecorder { func createRequest(
req := httptest.NewRequest(method, url, nil) method string,
url string,
token string,
body any,
) *httptest.ResponseRecorder {
var buf bytes.Buffer
if body != nil {
err := json.NewEncoder(&buf).Encode(body)
if err != nil {
log.Fatalf("could not encode body: %v", err)
}
}
req := httptest.NewRequest(method, url, &buf)
req.Header.Set(echo.HeaderContentType, echo.MIMEApplicationJSON) req.Header.Set(echo.HeaderContentType, echo.MIMEApplicationJSON)
req.Header.Set(echo.HeaderAuthorization, fmt.Sprintf("Bearer %s", token)) req.Header.Set(echo.HeaderAuthorization, fmt.Sprintf("Bearer %s", token))
res := httptest.NewRecorder() res := httptest.NewRecorder()