test: adding more scenario for each test

This commit is contained in:
mamad 2024-10-01 19:24:48 +03:30
parent bf99fbed6d
commit d4a4c36e7f
2 changed files with 97 additions and 20 deletions

View File

@ -23,16 +23,24 @@ import (
)
const (
agentPhoneNumber = "09384664403"
agentPassword = "Abc123456"
agentID uint = 4
agent1PhoneNumber = "09384664403"
agent1Password = "Abc123456"
agent1ID uint = 4
agent2PhoneNumber = "09384664404"
agent2Password = "Abc123456"
agent2ID uint = 5
)
func TestAgent_KindBox_Get(t *testing.T) {
t.Run("basic", Agent_KindBox_Get_Basic)
t.Run("no token", Agent_KindBox_Get_NoToken)
}
func Agent_KindBox_Get_Basic(t *testing.T) {
kindBox := entity.KindBox{
ID: 2,
SerialNumber: "serial-2",
ReceiverAgentID: agentID,
ReceiverAgentID: agent1ID,
Status: entity.KindBoxAssignedReceiverAgentStatus,
}
url := fmt.Sprintf("/agents/kindboxes/%d", kindBox.ID)
@ -40,7 +48,7 @@ func TestAgent_KindBox_Get(t *testing.T) {
teardown := setup.SeedMariaDB(testContainer.GetMariaDBConfig())
t.Cleanup(teardown)
token, err := getToken()
token, err := getToken(agent1PhoneNumber, agent1Password)
if err != nil {
t.Fatalf("could not login: %s", err)
}
@ -57,19 +65,33 @@ func TestAgent_KindBox_Get(t *testing.T) {
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 Agent_KindBox_Get_NoToken(t *testing.T) {
const kindBoxID = 2
url := fmt.Sprintf("/agents/kindboxes/%d", kindBoxID)
teardown := setup.SeedMariaDB(testContainer.GetMariaDBConfig())
t.Cleanup(teardown)
resRecord := createRequest(http.MethodGet, url, "", nil)
assert.Equal(t, http.StatusUnauthorized, resRecord.Code)
}
func TestAgent_KindBox_Return(t *testing.T) {
t.Run("basic", Agent_KindBox_Return_Basic)
t.Run("no token", Agent_KindBox_Return_NoToken)
t.Run("wrong agent", Agent_KindBox_Return_WrongAgent)
}
func Agent_KindBox_Return_Basic(t *testing.T) {
kindBoxReqBody := agentParam.ReturnKindBoxRequest{SerialNumber: "new-serial"}
var kindBoxID uint = 2
const kindBoxID uint = 2
url := fmt.Sprintf("/agents/kindboxes/return/%d", kindBoxID)
teardown := setup.SeedMariaDB(testContainer.GetMariaDBConfig())
t.Cleanup(teardown)
token, err := getToken()
token, err := getToken(agent1PhoneNumber, agent1Password)
if err != nil {
t.Fatalf("could not login: %s", err)
}
@ -82,16 +104,45 @@ func TestAgent_KindBox_Return(t *testing.T) {
assert.Equal(t, http.StatusNoContent, resRecord.Code)
assert.Equal(t, kindBoxReqBody.SerialNumber, updatedKindBox.SerialNumber)
assert.Equal(t, agentID, updatedKindBox.ReceiverAgentID)
assert.Equal(t, agent1ID, updatedKindBox.ReceiverAgentID)
assert.Equal(t, entity.KindBoxReturnedStatus, updatedKindBox.Status)
}
func getToken() (string, error) {
func Agent_KindBox_Return_NoToken(t *testing.T) {
const kindBoxID = 2
url := fmt.Sprintf("/agents/kindboxes/return/%d", kindBoxID)
teardown := setup.SeedMariaDB(testContainer.GetMariaDBConfig())
t.Cleanup(teardown)
resRecord := createRequest(http.MethodGet, url, "", nil)
assert.Equal(t, http.StatusUnauthorized, resRecord.Code)
}
func Agent_KindBox_Return_WrongAgent(t *testing.T) {
kindBoxReqBody := agentParam.ReturnKindBoxRequest{SerialNumber: "new-serial"}
const kindBoxID uint = 2
url := fmt.Sprintf("/agents/kindboxes/return/%d", kindBoxID)
teardown := setup.SeedMariaDB(testContainer.GetMariaDBConfig())
t.Cleanup(teardown)
token, err := getToken(agent2PhoneNumber, agent2Password)
if err != nil {
t.Fatalf("could not login: %s", err)
}
resRecord := createRequest(http.MethodPatch, url, token, kindBoxReqBody)
assert.Equal(t, http.StatusUnprocessableEntity, resRecord.Code)
}
func getToken(phoneNumber, password string) (string, error) {
res, err := services.AdminSvc.LoginWithPhoneNumber(
context.Background(),
adminserviceparam.LoginWithPhoneNumberRequest{
PhoneNumber: agentPhoneNumber,
Password: agentPassword,
PhoneNumber: phoneNumber,
Password: password,
},
)
if err != nil {
@ -100,10 +151,12 @@ func getToken() (string, error) {
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) {
t.Run("basic", Agent_kindBox_Get_All_Basic)
t.Run("no token", Agent_kindBox_Get_All_NoToken)
}
func Agent_kindBox_Get_All_Basic(t *testing.T) {
kindBoxesCount := 1
url := "/agents/kindboxes"
@ -111,7 +164,7 @@ func TestAgent_KindBox_Get_All(t *testing.T) {
teardown := setup.SeedMariaDB(testContainer.GetMariaDBConfig())
t.Cleanup(teardown)
token, err := getToken()
token, err := getToken(agent1PhoneNumber, agent1Password)
if err != nil {
t.Fatalf("could not login: %s", err)
}
@ -125,6 +178,17 @@ func TestAgent_KindBox_Get_All(t *testing.T) {
assert.Equal(t, kindBoxesCount, len(res.AllKindBoxes))
}
func Agent_kindBox_Get_All_NoToken(t *testing.T) {
url := "/agents/kindboxes"
teardown := setup.SeedMariaDB(testContainer.GetMariaDBConfig())
t.Cleanup(teardown)
resRecord := createRequest(http.MethodGet, url, "", nil)
assert.Equal(t, http.StatusUnauthorized, resRecord.Code)
}
func createRequest(
method string,
url string,

View File

@ -42,6 +42,19 @@ VALUES
'admin.agent.01@gmail.com',
'male',
'active'
),
(
5,
'ruhi',
'hosseini',
-- password is: Abc123456
'$2a$10$A4qiAep9OGYe2aFT3Tn1cuJfj99Xp6WA2F0SN9I7zAS01JGFU7XAO',
'09384664404',
'agent',
'testing',
'admin.agent.02@gmail.com',
'male',
'active'
);
-- +migrate Down