forked from ebhomengo/niki
Compare commits
4 Commits
develop
...
stage/mama
Author | SHA1 | Date |
---|---|---|
mamad | bf99fbed6d | |
mamad | f3eeb124f9 | |
mamad | d0aa6a6fe3 | |
mamad | 6c82aac02d |
|
@ -0,0 +1,150 @@
|
|||
//go:build end2end
|
||||
// +build end2end
|
||||
|
||||
package end2end
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"log"
|
||||
"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"
|
||||
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/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
const (
|
||||
agentPhoneNumber = "09384664403"
|
||||
agentPassword = "Abc123456"
|
||||
agentID uint = 4
|
||||
)
|
||||
|
||||
func TestAgent_KindBox_Get(t *testing.T) {
|
||||
kindBox := entity.KindBox{
|
||||
ID: 2,
|
||||
SerialNumber: "serial-2",
|
||||
ReceiverAgentID: agentID,
|
||||
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, nil)
|
||||
var res agentParam.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)
|
||||
}
|
||||
|
||||
// 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) {
|
||||
res, err := services.AdminSvc.LoginWithPhoneNumber(
|
||||
context.Background(),
|
||||
adminserviceparam.LoginWithPhoneNumberRequest{
|
||||
PhoneNumber: agentPhoneNumber,
|
||||
Password: agentPassword,
|
||||
},
|
||||
)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
return res.Tokens.AccessToken, nil
|
||||
}
|
||||
|
||||
// TODO: if anyone add a new kindbox for this agent, then this test will fail because count will change.
|
||||
// somehow we should link seed data with test data.
|
||||
// maybe not use sql seed data and create data in each test
|
||||
func TestAgent_KindBox_Get_All(t *testing.T) {
|
||||
kindBoxesCount := 1
|
||||
|
||||
url := "/agents/kindboxes"
|
||||
|
||||
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, nil)
|
||||
var res agentParam.GetAllResponse
|
||||
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, kindBoxesCount, len(res.AllKindBoxes))
|
||||
}
|
||||
|
||||
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)
|
||||
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
|
||||
}
|
|
@ -1,8 +1,10 @@
|
|||
-- +migrate Up
|
||||
INSERT INTO `kind_boxes` (`id`, `kind_box_req_id`, `benefactor_id`, `type`, `serial_number`, `status`, `deliver_refer_time_id`, `deliver_refer_date`, `deliver_address_id`, `sender_agent_id`, `delivered_at`)
|
||||
INSERT INTO `kind_boxes` (`id`, `kind_box_req_id`, `benefactor_id`, `type`, `serial_number`, `status`, `deliver_refer_time_id`, `deliver_refer_date`, `deliver_address_id`, `sender_agent_id`, `receiver_agent_id`, `delivered_at`)
|
||||
|
||||
VALUES
|
||||
-- delivered kindbox for benefactor 1
|
||||
(1, 1, 1, 'on-table', 'serial-1', 'delivered', 1, NOW() - INTERVAL 7 DAY, 1, 1, NOW());
|
||||
(1, 1, 1, 'on-table', 'serial-1', 'delivered', 1, NOW() - INTERVAL 7 DAY, 1, 1, NULL, NOW()),
|
||||
(2, 1, 1, 'on-table', 'serial-2', 'assigned-receiver-agent', 1, NOW() - INTERVAL 7 DAY, 1, 1, 4, NOW());
|
||||
|
||||
-- +migrate Down
|
||||
DELETE FROM `kind_boxes`;
|
||||
DELETE FROM `kind_boxes`;
|
||||
|
|
Loading…
Reference in New Issue