forked from ebhomengo/niki
1
0
Fork 0

Compare commits

...

1 Commits

Author SHA1 Message Date
Fatemeh Javadi 66ced2ea9b feat(admin): add e2e testing for kindbox by admin 2024-09-02 21:31:58 +03:30
2 changed files with 194 additions and 43 deletions

View File

@ -1,43 +0,0 @@
//go:build end2end
// +build end2end
package end2end
import (
"context"
"fmt"
"net/http"
"net/http/httptest"
"testing"
"git.gocasts.ir/ebhomengo/niki/delivery/http_server/end2end/setup"
adminserviceparam "git.gocasts.ir/ebhomengo/niki/param/admin/admin"
"github.com/labstack/echo/v4"
"github.com/stretchr/testify/assert"
)
func TestAdmin_KindBox_Get(t *testing.T) {
teardown := setup.SeedMariaDB(testContainer.GetMariaDBConfig())
t.Cleanup(teardown)
// Get token
lRes, err := services.AdminSvc.LoginWithPhoneNumber(context.Background(), adminserviceparam.LoginWithPhoneNumberRequest{
PhoneNumber: "09384664402",
Password: "Abc123456",
})
if err != nil {
t.Fatalf("could not login: %s", err)
}
// Create a request
req := httptest.NewRequest(http.MethodGet, "/admin/kindboxes/1", nil)
req.Header.Set(echo.HeaderContentType, echo.MIMEApplicationJSON)
req.Header.Set(echo.HeaderAuthorization, fmt.Sprintf("Bearer %s", lRes.Tokens.AccessToken))
rec := httptest.NewRecorder()
// Serve the request
testServer.Serve(rec, req)
// Assertions
assert.Equal(t, http.StatusOK, rec.Code)
}

View File

@ -0,0 +1,194 @@
//go:build end2end
// +build end2end
package end2end
import (
"context"
"encoding/json"
"fmt"
"net/http"
"net/http/httptest"
"strings"
"testing"
"git.gocasts.ir/ebhomengo/niki/delivery/http_server/end2end/setup"
httpmsg "git.gocasts.ir/ebhomengo/niki/pkg/http_msg"
adminserviceparam "git.gocasts.ir/ebhomengo/niki/param/admin/admin"
param "git.gocasts.ir/ebhomengo/niki/param/admin/kind_box"
"github.com/labstack/echo/v4"
"github.com/stretchr/testify/assert"
)
// Utility function to log in as an admin with dynamic credentials
func loginAsAdmin(t *testing.T, phoneNumber, password string) (*adminserviceparam.LoginWithPhoneNumberResponse, error) {
lRes, err := services.AdminSvc.LoginWithPhoneNumber(context.Background(), adminserviceparam.LoginWithPhoneNumberRequest{
PhoneNumber: phoneNumber,
Password: password,
})
if err != nil {
t.Fatalf("could not login: %s", err)
return nil, err
}
return &lRes, nil // Return address of lRes
}
func TestAdmin_KindBox_Enumerate(t *testing.T) {
teardown := setup.SeedMariaDB(testContainer.GetMariaDBConfig())
t.Cleanup(teardown)
lRes, err := loginAsAdmin(t, "09384664401", "Abc123456")
if err != nil {
return
}
reqBody := `{
"amount": 5
}`
req := httptest.NewRequest(http.MethodPatch, "/admin/kindboxes/1/enumerate", strings.NewReader(reqBody))
req.Header.Set(echo.HeaderContentType, echo.MIMEApplicationJSON)
req.Header.Set(echo.HeaderAuthorization, fmt.Sprintf("Bearer %s", lRes.Tokens.AccessToken))
rec := httptest.NewRecorder()
testServer.Serve(rec, req)
assert.Equal(t, http.StatusNoContent, rec.Code)
}
func TestAdmin_KindBox_Enumerate_MissingAmount(t *testing.T) {
teardown := setup.SeedMariaDB(testContainer.GetMariaDBConfig())
t.Cleanup(teardown)
lRes, err := loginAsAdmin(t, "09384664401", "Abc123456")
if err != nil {
return
}
reqBody := `{}`
req := httptest.NewRequest(http.MethodPatch, "/admin/kindboxes/1/enumerate", strings.NewReader(reqBody))
req.Header.Set(echo.HeaderContentType, echo.MIMEApplicationJSON)
req.Header.Set(echo.HeaderAuthorization, fmt.Sprintf("Bearer %s", lRes.Tokens.AccessToken))
rec := httptest.NewRecorder()
testServer.Serve(rec, req)
assert.Equal(t, http.StatusUnprocessableEntity, rec.Code)
}
func TestAdmin_KindBox_Enumerate_NegativeAmount(t *testing.T) {
teardown := setup.SeedMariaDB(testContainer.GetMariaDBConfig())
t.Cleanup(teardown)
lRes, err := loginAsAdmin(t, "09384664401", "Abc123456")
if err != nil {
return
}
reqBody := `{
"amount": -5
}`
req := httptest.NewRequest(http.MethodPatch, "/admin/kindboxes/1/enumerate", strings.NewReader(reqBody))
req.Header.Set(echo.HeaderContentType, echo.MIMEApplicationJSON)
req.Header.Set(echo.HeaderAuthorization, fmt.Sprintf("Bearer %s", lRes.Tokens.AccessToken))
rec := httptest.NewRecorder()
testServer.Serve(rec, req)
assert.Equal(t, http.StatusBadRequest, rec.Code)
}
func TestAdmin_KindBox_Enumeration_Forbidden(t *testing.T) {
lRes, err := loginAsAdmin(t, "09384664403", "Abc123456")
if err != nil {
return
}
req := httptest.NewRequest(http.MethodGet, "/admin/kindboxes/1/enumerate", nil)
req.Header.Set(echo.HeaderContentType, echo.MIMEApplicationJSON)
req.Header.Set(echo.HeaderAuthorization, fmt.Sprintf("Bearer %s", lRes.Tokens.AccessToken))
rec := httptest.NewRecorder()
testServer.Serve(rec, req)
assert.Equal(t, http.StatusForbidden, rec.Code)
var response httpmsg.ErrorResponse
err = json.NewDecoder(rec.Body).Decode(&response)
if err != nil {
t.Fatalf("could not decode response: %s", err)
}
}
func TestAdmin_KindBox_GetAll(t *testing.T) {
teardown := setup.SeedMariaDB(testContainer.GetMariaDBConfig())
t.Cleanup(teardown)
lRes, err := loginAsAdmin(t, "09384664401", "Abc123456")
if err != nil {
return
}
req := httptest.NewRequest(http.MethodGet, "/admin/kindboxes?page_number=1&page_size=10&sort=created_at&order=desc&filter=status:active", nil)
req.Header.Set(echo.HeaderAuthorization, fmt.Sprintf("Bearer %s", lRes.Tokens.AccessToken))
rec := httptest.NewRecorder()
testServer.Serve(rec, req)
assert.Equal(t, http.StatusOK, rec.Code)
var response param.KindBoxGetAllResponse
err = json.NewDecoder(rec.Body).Decode(&response)
if err != nil {
t.Fatalf("could not decode response: %s", err)
}
assert.NotEmpty(t, response.AllKindBox, "expected kind boxes in response")
assert.Equal(t, uint(1), response.Pagination.PageNumber, "expected page 1")
assert.Equal(t, uint(10), response.Pagination.PageSize, "expected limit 10")
assert.Empty(t, response.FieldErrors, "expected no field errors")
}
func TestAdmin_KindBox_AssignReceiver(t *testing.T) {
teardown := setup.SeedMariaDB(testContainer.GetMariaDBConfig())
t.Cleanup(teardown)
lRes, err := loginAsAdmin(t, "09384664401", "Abc123456")
if err != nil {
return
}
reqBody := `{
"receiver_agent_id": 4
}`
req := httptest.NewRequest(http.MethodPatch, "/admin/kindboxes/assign-receiver-agent/1", strings.NewReader(reqBody))
req.Header.Set(echo.HeaderContentType, echo.MIMEApplicationJSON)
req.Header.Set(echo.HeaderAuthorization, fmt.Sprintf("Bearer %s", lRes.Tokens.AccessToken))
rec := httptest.NewRecorder()
testServer.Serve(rec, req)
assert.Equal(t, http.StatusUnprocessableEntity, rec.Code)
}
func TestAdmin_KindBox_Get(t *testing.T) {
teardown := setup.SeedMariaDB(testContainer.GetMariaDBConfig())
t.Cleanup(teardown)
lRes, err := loginAsAdmin(t, "09384664401", "Abc123456")
if err != nil {
return
}
req := httptest.NewRequest(http.MethodGet, "/admin/kindboxes/1", nil)
req.Header.Set(echo.HeaderContentType, echo.MIMEApplicationJSON)
req.Header.Set(echo.HeaderAuthorization, fmt.Sprintf("Bearer %s", lRes.Tokens.AccessToken))
rec := httptest.NewRecorder()
testServer.Serve(rec, req)
assert.Equal(t, http.StatusOK, rec.Code)
}