forked from ebhomengo/niki
test: adding more scenario for each test
This commit is contained in:
parent
bf99fbed6d
commit
d4a4c36e7f
|
|
@ -23,16 +23,24 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
agentPhoneNumber = "09384664403"
|
agent1PhoneNumber = "09384664403"
|
||||||
agentPassword = "Abc123456"
|
agent1Password = "Abc123456"
|
||||||
agentID uint = 4
|
agent1ID uint = 4
|
||||||
|
agent2PhoneNumber = "09384664404"
|
||||||
|
agent2Password = "Abc123456"
|
||||||
|
agent2ID uint = 5
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestAgent_KindBox_Get(t *testing.T) {
|
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{
|
kindBox := entity.KindBox{
|
||||||
ID: 2,
|
ID: 2,
|
||||||
SerialNumber: "serial-2",
|
SerialNumber: "serial-2",
|
||||||
ReceiverAgentID: agentID,
|
ReceiverAgentID: agent1ID,
|
||||||
Status: entity.KindBoxAssignedReceiverAgentStatus,
|
Status: entity.KindBoxAssignedReceiverAgentStatus,
|
||||||
}
|
}
|
||||||
url := fmt.Sprintf("/agents/kindboxes/%d", kindBox.ID)
|
url := fmt.Sprintf("/agents/kindboxes/%d", kindBox.ID)
|
||||||
|
|
@ -40,7 +48,7 @@ func TestAgent_KindBox_Get(t *testing.T) {
|
||||||
teardown := setup.SeedMariaDB(testContainer.GetMariaDBConfig())
|
teardown := setup.SeedMariaDB(testContainer.GetMariaDBConfig())
|
||||||
t.Cleanup(teardown)
|
t.Cleanup(teardown)
|
||||||
|
|
||||||
token, err := getToken()
|
token, err := getToken(agent1PhoneNumber, agent1Password)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("could not login: %s", err)
|
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)
|
assert.Equal(t, kindBox.Status, res.Status)
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: we can add more tests for return.
|
func Agent_KindBox_Get_NoToken(t *testing.T) {
|
||||||
// we can get check if the data is updated in db as expects
|
const kindBoxID = 2
|
||||||
// or api should return error if the kindbox is not for this agent
|
url := fmt.Sprintf("/agents/kindboxes/%d", kindBoxID)
|
||||||
// CHECK: is the flow correct? agent can change kindbox status to return no matter what was the previous status?
|
|
||||||
|
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) {
|
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"}
|
kindBoxReqBody := agentParam.ReturnKindBoxRequest{SerialNumber: "new-serial"}
|
||||||
var kindBoxID uint = 2
|
const kindBoxID uint = 2
|
||||||
url := fmt.Sprintf("/agents/kindboxes/return/%d", kindBoxID)
|
url := fmt.Sprintf("/agents/kindboxes/return/%d", kindBoxID)
|
||||||
|
|
||||||
teardown := setup.SeedMariaDB(testContainer.GetMariaDBConfig())
|
teardown := setup.SeedMariaDB(testContainer.GetMariaDBConfig())
|
||||||
t.Cleanup(teardown)
|
t.Cleanup(teardown)
|
||||||
|
|
||||||
token, err := getToken()
|
token, err := getToken(agent1PhoneNumber, agent1Password)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("could not login: %s", err)
|
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, http.StatusNoContent, resRecord.Code)
|
||||||
assert.Equal(t, kindBoxReqBody.SerialNumber, updatedKindBox.SerialNumber)
|
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)
|
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(
|
res, err := services.AdminSvc.LoginWithPhoneNumber(
|
||||||
context.Background(),
|
context.Background(),
|
||||||
adminserviceparam.LoginWithPhoneNumberRequest{
|
adminserviceparam.LoginWithPhoneNumberRequest{
|
||||||
PhoneNumber: agentPhoneNumber,
|
PhoneNumber: phoneNumber,
|
||||||
Password: agentPassword,
|
Password: password,
|
||||||
},
|
},
|
||||||
)
|
)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
@ -100,10 +151,12 @@ func getToken() (string, error) {
|
||||||
return res.Tokens.AccessToken, nil
|
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) {
|
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
|
kindBoxesCount := 1
|
||||||
|
|
||||||
url := "/agents/kindboxes"
|
url := "/agents/kindboxes"
|
||||||
|
|
@ -111,7 +164,7 @@ func TestAgent_KindBox_Get_All(t *testing.T) {
|
||||||
teardown := setup.SeedMariaDB(testContainer.GetMariaDBConfig())
|
teardown := setup.SeedMariaDB(testContainer.GetMariaDBConfig())
|
||||||
t.Cleanup(teardown)
|
t.Cleanup(teardown)
|
||||||
|
|
||||||
token, err := getToken()
|
token, err := getToken(agent1PhoneNumber, agent1Password)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("could not login: %s", err)
|
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))
|
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(
|
func createRequest(
|
||||||
method string,
|
method string,
|
||||||
url string,
|
url string,
|
||||||
|
|
|
||||||
|
|
@ -42,7 +42,20 @@ VALUES
|
||||||
'admin.agent.01@gmail.com',
|
'admin.agent.01@gmail.com',
|
||||||
'male',
|
'male',
|
||||||
'active'
|
'active'
|
||||||
|
),
|
||||||
|
(
|
||||||
|
5,
|
||||||
|
'ruhi',
|
||||||
|
'hosseini',
|
||||||
|
-- password is: Abc123456
|
||||||
|
'$2a$10$A4qiAep9OGYe2aFT3Tn1cuJfj99Xp6WA2F0SN9I7zAS01JGFU7XAO',
|
||||||
|
'09384664404',
|
||||||
|
'agent',
|
||||||
|
'testing',
|
||||||
|
'admin.agent.02@gmail.com',
|
||||||
|
'male',
|
||||||
|
'active'
|
||||||
);
|
);
|
||||||
|
|
||||||
-- +migrate Down
|
-- +migrate Down
|
||||||
DELETE FROM `admins` WHERE id != 1;
|
DELETE FROM `admins` WHERE id != 1;
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue