From 22239246f318556388de6f5bfb4550b8ebcf382d Mon Sep 17 00:00:00 2001 From: Zibbp Date: Thu, 12 Dec 2024 00:40:09 +0000 Subject: [PATCH] remove unused tests --- internal/auth/auth_test.go | 73 ------------ internal/transport/http/auth_test.go | 164 --------------------------- 2 files changed, 237 deletions(-) delete mode 100644 internal/auth/auth_test.go delete mode 100644 internal/transport/http/auth_test.go diff --git a/internal/auth/auth_test.go b/internal/auth/auth_test.go deleted file mode 100644 index 2bbf68f..0000000 --- a/internal/auth/auth_test.go +++ /dev/null @@ -1,73 +0,0 @@ -package auth_test - -import ( - "context" - "encoding/base64" - "encoding/json" - "net/http/httptest" - "strings" - "testing" - - "github.com/golang-jwt/jwt/v4" - "github.com/labstack/echo/v4" - "github.com/stretchr/testify/assert" - "github.com/zibbp/ganymede/internal/user" - "github.com/zibbp/ganymede/tests" -) - -func TestRegister(t *testing.T) { - ctx := context.Background() - app, err := tests.Setup(t) - assert.NoError(t, err) - - // test Register - usr, err := app.AuthService.Register(ctx, user.User{Username: "test_user", Password: "password"}) - assert.NoError(t, err) - assert.Equal(t, "test_user", usr.Username) -} - -func TestLogin(t *testing.T) { - ctx := context.Background() - app, err := tests.Setup(t) - assert.NoError(t, err) - - e := echo.New() - req := httptest.NewRequest("POST", "/api/v1/auth/login", nil) - rec := httptest.NewRecorder() - - echoCtx := e.NewContext(req, rec) - - _, err = app.AuthService.Register(ctx, user.User{Username: "test_user", Password: "password"}) - assert.NoError(t, err) - - // test Login - usr, err := app.AuthService.Login(echoCtx, user.User{Username: "admin", Password: "ganymede"}) - assert.NoError(t, err) - assert.Equal(t, "admin", usr.Username) - - setCookies := rec.Header().Values("Set-Cookie") - - // test cookies are valid jwt tokens - for _, cookie := range setCookies { - // example cookie: - // refresh-token=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VyX2lkIjoiNmRmNWFiNDctMzNiOC00ZWFjLWE2M2QtYjlhZjhlMmRiNWRjIiwidXNlcm5hbWUiOiJhZG1pbiIsInJvbGUiOiJhZG1pbiIsImV4cCI6MTcyNTg1NDM0NX0.wltMCYWMwbV6BqU2PM7PLIWIy9uqJmGN5N50oNLpWSY; Path=/; Expires=Mon, 09 Sep 2024 03:59:05 GMT; SameSite=Lax - split := strings.Split(cookie, ";") - token := strings.Split(split[0], "=")[1] - - assert.NotEmpty(t, token) - - parts := strings.Split(token, ".") - - assert.Equal(t, 3, len(parts)) - - payload, err := base64.RawURLEncoding.DecodeString(parts[1]) - assert.NoError(t, err) - - var claims jwt.MapClaims - err = json.Unmarshal(payload, &claims) - assert.NoError(t, err) - assert.Equal(t, usr.ID.String(), claims["user_id"]) - assert.Equal(t, usr.Username, claims["username"]) - assert.Equal(t, string(usr.Role), claims["role"]) - } -} diff --git a/internal/transport/http/auth_test.go b/internal/transport/http/auth_test.go deleted file mode 100644 index bf6ac96..0000000 --- a/internal/transport/http/auth_test.go +++ /dev/null @@ -1,164 +0,0 @@ -package http_test - -import ( - "bytes" - "context" - "encoding/json" - "net/http" - "net/http/httptest" - "testing" - - "github.com/labstack/echo/v4" - "github.com/stretchr/testify/assert" - "github.com/stretchr/testify/mock" - "github.com/zibbp/ganymede/ent" - "github.com/zibbp/ganymede/internal/auth" - httpHandler "github.com/zibbp/ganymede/internal/transport/http" - "github.com/zibbp/ganymede/internal/user" -) - -type MockAuthService struct { - mock.Mock -} - -func (m *MockAuthService) Register(ctx context.Context, userDto user.User) (*ent.User, error) { - args := m.Called(ctx, userDto) - return args.Get(0).(*ent.User), args.Error(1) -} - -func (m *MockAuthService) Login(c echo.Context, userDto user.User) (*ent.User, error) { - args := m.Called(c, userDto) - return args.Get(0).(*ent.User), args.Error(1) -} - -func (m *MockAuthService) Refresh(c echo.Context, refreshToken string) error { - args := m.Called(c, refreshToken) - return args.Error(0) -} - -func (m *MockAuthService) Me(c *auth.CustomContext) (*ent.User, error) { - args := m.Called(c) - return args.Get(0).(*ent.User), args.Error(1) -} - -func (m *MockAuthService) ChangePassword(c *auth.CustomContext, passwordDto auth.ChangePassword) error { - args := m.Called(c, passwordDto) - return args.Error(0) -} - -func (m *MockAuthService) OAuthRedirect(c echo.Context) error { - args := m.Called(c) - return args.Error(0) -} - -func (m *MockAuthService) OAuthCallback(c echo.Context) error { - args := m.Called(c) - return args.Error(0) -} - -func (m *MockAuthService) OAuthTokenRefresh(c echo.Context, refreshToken string) error { - args := m.Called(c, refreshToken) - return args.Error(0) -} - -func (m *MockAuthService) OAuthLogout(c echo.Context) error { - args := m.Called(c) - return args.Error(0) -} - -func setupAuthHandler() *httpHandler.Handler { - e := setupEcho() - mockAuthService := new(MockAuthService) - - services := httpHandler.Services{ - AuthService: mockAuthService, - } - - handler := &httpHandler.Handler{ - Server: e, - Service: services, - } - - return handler -} - -func TestRegister(t *testing.T) { - handler := setupAuthHandler() - e := handler.Server - mockService := handler.Service.AuthService.(*MockAuthService) - - // test register - registerBody := httpHandler.RegisterRequest{ - Username: "username", - Password: "password", - } - - expectedInput := user.User{ - Username: "username", - Password: "password", - } - - expectedOutput := &ent.User{ - Username: "username", - } - - mockService.On("Register", mock.Anything, expectedInput).Return(expectedOutput, nil) - - b, err := json.Marshal(registerBody) - if err != nil { - t.Fatal(err) - } - req := httptest.NewRequest(http.MethodPost, "/auth/register", bytes.NewBuffer(b)) - req.Header.Set(echo.HeaderContentType, echo.MIMEApplicationJSON) - rec := httptest.NewRecorder() - c := e.NewContext(req, rec) - - if assert.NoError(t, handler.Register(c)) { - assert.Equal(t, http.StatusOK, rec.Code) - var response *ent.User - err := json.Unmarshal(rec.Body.Bytes(), &response) - assert.NoError(t, err) - assert.Equal(t, expectedOutput, response) - } -} - -// TestLogin is a test function for login. -func TestLogin(t *testing.T) { - handler := setupAuthHandler() - e := handler.Server - mockService := handler.Service.AuthService.(*MockAuthService) - - // test login - loginBody := httpHandler.LoginRequest{ - Username: "username", - Password: "password", - } - - expectedInput := user.User{ - Username: "username", - Password: "password", - } - - expectedOutput := &ent.User{ - Username: "username", - } - - mockService.On("Login", mock.Anything, expectedInput).Return(expectedOutput, nil) - - b, err := json.Marshal(loginBody) - if err != nil { - t.Fatal(err) - } - req := httptest.NewRequest(http.MethodPost, "/auth/login", bytes.NewBuffer(b)) - req.Header.Set(echo.HeaderContentType, echo.MIMEApplicationJSON) - rec := httptest.NewRecorder() - c := e.NewContext(req, rec) - - if assert.NoError(t, handler.Login(c)) { - assert.Equal(t, http.StatusOK, rec.Code) - var response *ent.User - err := json.Unmarshal(rec.Body.Bytes(), &response) - assert.NoError(t, err) - assert.Equal(t, expectedOutput, response) - } -}