127 lines
4.8 KiB
Go
127 lines
4.8 KiB
Go
package httpserver
|
|
|
|
import (
|
|
"Rezina-Backend/internal/domain"
|
|
"Rezina-Backend/internal/ports"
|
|
userservice "Rezina-Backend/internal/services/user"
|
|
"Rezina-Backend/internal/web_server/handlers"
|
|
jwtutil "Rezina-Backend/internal/web_server/jwt"
|
|
"context"
|
|
"github.com/gofiber/fiber/v2"
|
|
"go.uber.org/zap"
|
|
"log/slog"
|
|
"net/http/httptest"
|
|
"testing"
|
|
)
|
|
|
|
type audienceUserStore struct {
|
|
ports.UserStore
|
|
status domain.UserStatus
|
|
}
|
|
|
|
func (s audienceUserStore) GetUserByID(context.Context, int64) (domain.User, error) {
|
|
return domain.User{ID: 42, Status: s.status}, nil
|
|
}
|
|
func audienceTestApp(status domain.UserStatus) *App {
|
|
return &App{JwtConfig: jwtutil.JwtConfig{JwtAccessKey: "audience-test-key"}, mongoLoggerSvc: zap.NewNop(), logger: slog.Default(), userSvc: userservice.NewService(nil, audienceUserStore{status: status}, nil, nil, nil, nil, nil)}
|
|
}
|
|
func audienceToken(t *testing.T, a *App, role domain.Role) string {
|
|
t.Helper()
|
|
token, err := jwtutil.CreateJwt(42, role, a.JwtConfig.JwtAccessKey, 3600)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
return token
|
|
}
|
|
func TestAPIAudienceAuthentication(t *testing.T) {
|
|
for _, tc := range []struct {
|
|
name, audience string
|
|
role domain.Role
|
|
status domain.UserStatus
|
|
want int
|
|
}{
|
|
{"student client", "client", domain.RoleStudent, domain.UserStatusActive, 204},
|
|
{"open learner client", "client", domain.RoleOpenLearner, domain.UserStatusActive, 204},
|
|
{"admin panel", "admin", domain.RoleAdmin, domain.UserStatusActive, 204},
|
|
{"content manager panel", "admin", domain.RoleContentManager, domain.UserStatusActive, 204},
|
|
{"instructor panel", "admin", domain.RoleInstructor, domain.UserStatusActive, 204},
|
|
{"student cannot administer", "admin", domain.RoleStudent, domain.UserStatusActive, 403},
|
|
{"open learner cannot administer", "admin", domain.RoleOpenLearner, domain.UserStatusActive, 403},
|
|
{"admin cannot act as customer", "client", domain.RoleAdmin, domain.UserStatusActive, 403},
|
|
{"instructor cannot act as customer", "client", domain.RoleInstructor, domain.UserStatusActive, 403},
|
|
{"suspended customer", "client", domain.RoleStudent, domain.UserStatusSuspended, 403},
|
|
{"unknown role", "admin", domain.Role("UNKNOWN"), domain.UserStatusActive, 403},
|
|
} {
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
a := audienceTestApp(tc.status)
|
|
server := fiber.New()
|
|
auth := a.adminAuthMiddleware
|
|
if tc.audience == "client" {
|
|
auth = a.clientAuthMiddleware
|
|
}
|
|
server.Get("/me", auth, func(c *fiber.Ctx) error { return c.SendStatus(204) })
|
|
request := httptest.NewRequest("GET", "/me", nil)
|
|
request.Header.Set("Authorization", "Bearer "+audienceToken(t, a, tc.role))
|
|
response, err := server.Test(request)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
defer response.Body.Close()
|
|
if response.StatusCode != tc.want {
|
|
t.Fatalf("status=%d want=%d", response.StatusCode, tc.want)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
func TestRegisteredAPISurfaces(t *testing.T) {
|
|
a := audienceTestApp(domain.UserStatusActive)
|
|
server := fiber.New()
|
|
h := &handlers.Handler{}
|
|
api := server.Group("/api/v1")
|
|
a.initAdminRoutes(api.Group("/admin", apiAudience("admin")), h)
|
|
a.initClientRoutes(api, h)
|
|
a.initIntegrationRoutes(api, h)
|
|
routes := map[string]bool{}
|
|
for _, stack := range server.Stack() {
|
|
for _, route := range stack {
|
|
routes[route.Method+" "+route.Path] = true
|
|
}
|
|
}
|
|
for _, route := range []string{"POST /api/v1/admin/exam-prep/catalog-courses", "GET /api/v1/exam-prep/catalog-courses", "POST /api/v1/admin/team/login", "POST /api/v1/user/register", "GET /api/v1/admin/users", "GET /api/v1/referrals/me", "POST /api/v1/payments/webhook", "GET /api/v1/payments/chapa/callback", "GET /api/v1/auth/google/callback"} {
|
|
if !routes[route] {
|
|
t.Errorf("missing route %s", route)
|
|
}
|
|
}
|
|
for _, route := range []string{"POST /api/v1/exam-prep/catalog-courses", "GET /api/v1/questions", "GET /api/v1/users", "GET /api/v1/rbac/roles", "POST /api/v1/team/login", "GET /api/v1/questions", "POST /api/v1/admin/user/register", "GET /api/v1/client/subscriptions/me", "POST /api/v1/admin/subscriptions/checkout"} {
|
|
if routes[route] {
|
|
t.Errorf("unexpected route %s", route)
|
|
}
|
|
}
|
|
for _, tc := range []struct {
|
|
path string
|
|
role domain.Role
|
|
}{
|
|
{"/api/v1/admin/users", domain.RoleStudent},
|
|
{"/api/v1/admin/exam-prep/catalog-courses", domain.RoleOpenLearner},
|
|
{"/api/v1/subscriptions/me", domain.RoleAdmin},
|
|
{"/api/v1/admin/ws/connect?token=", domain.RoleStudent},
|
|
{"/api/v1/ws/connect?token=", domain.RoleAdmin},
|
|
} {
|
|
token := audienceToken(t, a, tc.role)
|
|
path := tc.path
|
|
if path[len(path)-1:] == "=" {
|
|
path += token
|
|
}
|
|
request := httptest.NewRequest("GET", path, nil)
|
|
request.Header.Set("Authorization", "Bearer "+token)
|
|
response, err := server.Test(request)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
response.Body.Close()
|
|
if response.StatusCode != 403 {
|
|
t.Errorf("%s returned %d", tc.path, response.StatusCode)
|
|
}
|
|
}
|
|
}
|