2024-09-02 11:58:58 +00:00
|
|
|
//go:build end2end
|
|
|
|
// +build end2end
|
|
|
|
|
|
|
|
package end2end
|
|
|
|
|
|
|
|
import (
|
2024-09-02 15:26:22 +00:00
|
|
|
"bytes"
|
2024-09-02 11:58:58 +00:00
|
|
|
"context"
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
2024-09-02 15:26:22 +00:00
|
|
|
"log"
|
2024-09-02 11:58:58 +00:00
|
|
|
"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"
|
2024-09-02 15:26:22 +00:00
|
|
|
adminParam "git.gocasts.ir/ebhomengo/niki/param/admin/kind_box"
|
|
|
|
agentParam "git.gocasts.ir/ebhomengo/niki/param/agent/kind_box"
|
2024-09-02 11:58:58 +00:00
|
|
|
"github.com/labstack/echo/v4"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2024-09-02 15:26:22 +00:00
|
|
|
agentPhoneNumber = "09384664403"
|
|
|
|
agentPassword = "Abc123456"
|
|
|
|
agentID uint = 4
|
2024-09-02 11:58:58 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestAgent_KindBox_Get(t *testing.T) {
|
|
|
|
kindBox := entity.KindBox{
|
|
|
|
ID: 2,
|
|
|
|
SerialNumber: "serial-2",
|
2024-09-02 15:26:22 +00:00
|
|
|
ReceiverAgentID: agentID,
|
2024-09-02 11:58:58 +00:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2024-09-02 15:26:22 +00:00
|
|
|
resRecord := createRequest(http.MethodGet, url, token, nil)
|
|
|
|
var res agentParam.GetKindBoxResponse
|
2024-09-02 11:58:58 +00:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2024-09-02 15:26:22 +00:00
|
|
|
// 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)
|
|
|
|
}
|
|
|
|
|
2024-09-02 11:58:58 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2024-09-02 15:26:22 +00:00
|
|
|
func createRequest(
|
|
|
|
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)
|
2024-09-02 11:58:58 +00:00
|
|
|
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
|
|
|
|
}
|