diff --git a/internal/webserver/highlights_test.go b/internal/webserver/highlights_test.go index 7f594d0..f4fd5d0 100644 --- a/internal/webserver/highlights_test.go +++ b/internal/webserver/highlights_test.go @@ -37,7 +37,7 @@ func TestHighlights(t *testing.T) { t.Fatalf("Unexpected error: %v", err.Error()) } - response, err := addUser(regularUserData, adminCookie, app) + response, err := postRequest(regularUserData, adminCookie, app, "/en/users/new") if response == nil { t.Fatalf("Unexpected error: %v", err.Error()) } @@ -98,7 +98,7 @@ func TestHighlights(t *testing.T) { "uuid": {regularUser.Uuid}, } - _, err = deleteUser(data, adminCookie, app) + _, err = postRequest(data, adminCookie, app, "/en/users/delete") if err != nil { t.Fatalf("Unexpected error: %v", err.Error()) } diff --git a/internal/webserver/upload_test.go b/internal/webserver/upload_test.go new file mode 100644 index 0000000..be465f7 --- /dev/null +++ b/internal/webserver/upload_test.go @@ -0,0 +1,38 @@ +package webserver_test + +import ( + "net/http" + "testing" + + "github.com/spf13/afero" + "github.com/svera/coreander/v3/internal/webserver/infrastructure" +) + +func TestUpload(t *testing.T) { + db := infrastructure.Connect("file::memory:", 250) + app := bootstrapApp(db, &infrastructure.NoEmail{}, afero.NewMemMapFs()) + + adminCookie, err := login(app, "admin@example.com", "admin") + if err != nil { + t.Fatalf("Unexpected error: %v", err.Error()) + } + + t.Run("Try to access upload page without an active session", func(t *testing.T) { + response, err := getRequest(&http.Cookie{}, app, "/en/upload") + if response == nil { + t.Fatalf("Unexpected error: %v", err.Error()) + } + + mustReturnForbiddenAndShowLogin(response, t) + }) + + t.Run("Try to add a user with an admin active session", func(t *testing.T) { + response, err := getRequest(adminCookie, app, "/en/upload") + if response == nil { + t.Fatalf("Unexpected error: %v", err.Error()) + } + if expectedStatus := http.StatusOK; response.StatusCode != expectedStatus { + t.Errorf("Expected status %d, got %d", expectedStatus, response.StatusCode) + } + }) +} diff --git a/internal/webserver/user_management_test.go b/internal/webserver/user_management_test.go index 69c47fd..26003ab 100644 --- a/internal/webserver/user_management_test.go +++ b/internal/webserver/user_management_test.go @@ -34,14 +34,14 @@ func TestUserManagement(t *testing.T) { } t.Run("Try to add a user without an active session", func(t *testing.T) { - response, err := newUser(&http.Cookie{}, app) + response, err := getRequest(&http.Cookie{}, app, "/en/users/new") if response == nil { t.Fatalf("Unexpected error: %v", err.Error()) } mustReturnForbiddenAndShowLogin(response, t) - response, err = addUser(data, &http.Cookie{}, app) + response, err = postRequest(data, &http.Cookie{}, app, "/en/users/new") if response == nil { t.Fatalf("Unexpected error: %v", err.Error()) } @@ -50,14 +50,14 @@ func TestUserManagement(t *testing.T) { }) t.Run("Try to add a user with an admin active session", func(t *testing.T) { - response, err := newUser(adminCookie, app) + response, err := getRequest(adminCookie, app, "/en/users/new") if response == nil { t.Fatalf("Unexpected error: %v", err.Error()) } mustReturnStatus(response, fiber.StatusOK, t) - response, err = addUser(data, adminCookie, app) + response, err = postRequest(data, adminCookie, app, "/en/users/new") if response == nil { t.Fatalf("Unexpected error: %v", err.Error()) } @@ -72,7 +72,7 @@ func TestUserManagement(t *testing.T) { }) t.Run("Try to add a user with errors in form using an admin active session", func(t *testing.T) { - response, err := addUser(url.Values{}, adminCookie, app) + response, err := postRequest(url.Values{}, adminCookie, app, "/en/users/new") expectedErrorMessages := []string{ "Name cannot be empty", "Incorrect email address", @@ -103,7 +103,7 @@ func TestUserManagement(t *testing.T) { t.Fatalf("Unexpected error: %v", err.Error()) } - response, err := addUser(data, cookie, app) + response, err := postRequest(data, cookie, app, "/en/users/new") if response == nil { t.Fatalf("Unexpected error: %v", err.Error()) } @@ -123,14 +123,14 @@ func TestUserManagement(t *testing.T) { } t.Run("Try to update a user without an active session", func(t *testing.T) { - response, err := editUser(testUser.Uuid, &http.Cookie{}, app) + response, err := getRequest(&http.Cookie{}, app, fmt.Sprintf("/en/users/%s/edit", testUser.Uuid)) if response == nil { t.Fatalf("Unexpected error: %v", err.Error()) } mustReturnForbiddenAndShowLogin(response, t) - response, err = updateUser(testUser.Uuid, data, &http.Cookie{}, app) + response, err = postRequest(data, &http.Cookie{}, app, fmt.Sprintf("/en/users/%s/edit", testUser.Uuid)) if response == nil { t.Fatalf("Unexpected error: %v", err.Error()) } @@ -139,14 +139,14 @@ func TestUserManagement(t *testing.T) { }) t.Run("Try to update a user using another, non admin user session", func(t *testing.T) { - response, err := editUser(adminUser.Uuid, testUserCookie, app) + response, err := getRequest(testUserCookie, app, fmt.Sprintf("/en/users/%s/edit", adminUser.Uuid)) if response == nil { t.Fatalf("Unexpected error: %v", err.Error()) } mustReturnStatus(response, fiber.StatusForbidden, t) - response, err = updateUser(adminUser.Uuid, data, testUserCookie, app) + response, err = postRequest(data, testUserCookie, app, fmt.Sprintf("/en/users/%s/edit", adminUser.Uuid)) if response == nil { t.Fatalf("Unexpected error: %v", err.Error()) } @@ -157,14 +157,14 @@ func TestUserManagement(t *testing.T) { t.Run("Try to update the user in session", func(t *testing.T) { data.Set("name", "Updated test user") - response, err := editUser(testUser.Uuid, testUserCookie, app) + response, err := getRequest(testUserCookie, app, fmt.Sprintf("/en/users/%s/edit", testUser.Uuid)) if response == nil { t.Fatalf("Unexpected error: %v", err.Error()) } mustReturnStatus(response, fiber.StatusOK, t) - response, err = updateUser(testUser.Uuid, data, testUserCookie, app) + response, err = postRequest(data, testUserCookie, app, fmt.Sprintf("/en/users/%s/edit", testUser.Uuid)) if response == nil { t.Fatalf("Unexpected error: %v", err.Error()) } @@ -178,14 +178,14 @@ func TestUserManagement(t *testing.T) { t.Run("Try to update a user with an admin session", func(t *testing.T) { data.Set("name", "Updated test user by an admin") - response, err := updateUser(testUser.Uuid, data, adminCookie, app) + response, err := postRequest(data, adminCookie, app, fmt.Sprintf("/en/users/%s/edit", testUser.Uuid)) if response == nil { t.Fatalf("Unexpected error: %v", err.Error()) } mustReturnStatus(response, fiber.StatusOK, t) - response, err = updateUser(testUser.Uuid, data, adminCookie, app) + response, err = postRequest(data, adminCookie, app, fmt.Sprintf("/en/users/%s/edit", testUser.Uuid)) if response == nil { t.Fatalf("Unexpected error: %v", err.Error()) } @@ -197,7 +197,7 @@ func TestUserManagement(t *testing.T) { }) t.Run("Try to edit a non existing user with an admin session", func(t *testing.T) { - response, err := editUser("abcde", adminCookie, app) + response, err := getRequest(adminCookie, app, fmt.Sprintf("/en/users/%s/edit", "abcde")) if response == nil { t.Fatalf("Unexpected error: %v", err.Error()) } @@ -211,7 +211,7 @@ func TestUserManagement(t *testing.T) { t.Run("Try to update a non existing user with an admin session", func(t *testing.T) { data.Set("name", "Updated test user by an admin") - response, err := updateUser("abcde", data, adminCookie, app) + response, err := postRequest(data, adminCookie, app, fmt.Sprintf("/en/users/%s/edit", "abcde")) if response == nil { t.Fatalf("Unexpected error: %v", err.Error()) } @@ -223,7 +223,7 @@ func TestUserManagement(t *testing.T) { } t.Run("Try to delete a user without an active session", func(t *testing.T) { - response, err := deleteUser(data, &http.Cookie{}, app) + response, err := postRequest(data, &http.Cookie{}, app, "/en/users/delete") if response == nil { t.Fatalf("Unexpected error: %v", err.Error()) } @@ -234,7 +234,7 @@ func TestUserManagement(t *testing.T) { t.Run("Try to delete a user with a regular user's session", func(t *testing.T) { data.Set("name", "Updated test user") - response, err := deleteUser(data, testUserCookie, app) + response, err := postRequest(data, testUserCookie, app, "/en/users/delete") if response == nil { t.Fatalf("Unexpected error: %v", err.Error()) } @@ -243,7 +243,7 @@ func TestUserManagement(t *testing.T) { }) t.Run("Try to delete a user with an admin session", func(t *testing.T) { - response, err := deleteUser(data, adminCookie, app) + response, err := postRequest(data, adminCookie, app, "/en/users/delete") if response == nil { t.Fatalf("Unexpected error: %v", err.Error()) } @@ -259,7 +259,7 @@ func TestUserManagement(t *testing.T) { data = url.Values{ "uuid": {adminUser.Uuid}, } - response, err := deleteUser(data, adminCookie, app) + response, err := postRequest(data, adminCookie, app, "/en/users/delete") if response == nil { t.Fatalf("Unexpected error: %v", err.Error()) } @@ -306,59 +306,6 @@ func mustReturnStatus(response *http.Response, expectedStatus int, t *testing.T) } } -func newUser(cookie *http.Cookie, app *fiber.App) (*http.Response, error) { - req, err := http.NewRequest(http.MethodGet, "/en/users/new", nil) - if err != nil { - return nil, err - } - req.AddCookie(cookie) - - return app.Test(req) -} - -func addUser(data url.Values, cookie *http.Cookie, app *fiber.App) (*http.Response, error) { - req, err := http.NewRequest(http.MethodPost, "/en/users/new", strings.NewReader(data.Encode())) - if err != nil { - return nil, err - } - req.Header.Add("Content-Type", "application/x-www-form-urlencoded") - req.AddCookie(cookie) - - return app.Test(req) -} - -func editUser(uuid string, cookie *http.Cookie, app *fiber.App) (*http.Response, error) { - req, err := http.NewRequest(http.MethodGet, fmt.Sprintf("/en/users/%s/edit", uuid), nil) - if err != nil { - return nil, err - } - req.AddCookie(cookie) - - return app.Test(req) -} - -func updateUser(uuid string, data url.Values, cookie *http.Cookie, app *fiber.App) (*http.Response, error) { - req, err := http.NewRequest(http.MethodPost, fmt.Sprintf("/en/users/%s/edit", uuid), strings.NewReader(data.Encode())) - if err != nil { - return nil, err - } - req.Header.Add("Content-Type", "application/x-www-form-urlencoded") - req.AddCookie(cookie) - - return app.Test(req) -} - -func deleteUser(data url.Values, cookie *http.Cookie, app *fiber.App) (*http.Response, error) { - req, err := http.NewRequest(http.MethodPost, "/en/users/delete", strings.NewReader(data.Encode())) - if err != nil { - return nil, err - } - req.Header.Add("Content-Type", "application/x-www-form-urlencoded") - req.AddCookie(cookie) - - return app.Test(req) -} - func login(app *fiber.App, email, password string) (*http.Cookie, error) { data := url.Values{ "email": {email}, diff --git a/internal/webserver/webserver_test.go b/internal/webserver/webserver_test.go index 729a5f3..dde8387 100644 --- a/internal/webserver/webserver_test.go +++ b/internal/webserver/webserver_test.go @@ -3,6 +3,8 @@ package webserver_test import ( "log" "net/http" + "net/url" + "strings" "sync" "testing" "time" @@ -106,3 +108,24 @@ func (s *SMTPMock) SendDocument(address string, libraryPath string, fileName str func (s *SMTPMock) From() string { return "" } + +func getRequest(cookie *http.Cookie, app *fiber.App, URL string) (*http.Response, error) { + req, err := http.NewRequest(http.MethodGet, URL, nil) + if err != nil { + return nil, err + } + req.AddCookie(cookie) + + return app.Test(req) +} + +func postRequest(data url.Values, cookie *http.Cookie, app *fiber.App, URL string) (*http.Response, error) { + req, err := http.NewRequest(http.MethodPost, URL, strings.NewReader(data.Encode())) + if err != nil { + return nil, err + } + req.Header.Add("Content-Type", "application/x-www-form-urlencoded") + req.AddCookie(cookie) + + return app.Test(req) +}