forked from ebhomengo/niki
1
0
Fork 0
niki/delivery/http_server/end2end/admin_kindbox_test.go

195 lines
5.7 KiB
Go
Raw Normal View History

//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)
}