Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
svera committed Feb 11, 2024
1 parent 7821862 commit d02ecbe
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 75 deletions.
4 changes: 2 additions & 2 deletions internal/webserver/highlights_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
}
Expand Down Expand Up @@ -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())
}
Expand Down
38 changes: 38 additions & 0 deletions internal/webserver/upload_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
})
}
93 changes: 20 additions & 73 deletions internal/webserver/user_management_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
}
Expand All @@ -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())
}
Expand All @@ -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",
Expand Down Expand Up @@ -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())
}
Expand All @@ -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())
}
Expand All @@ -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())
}
Expand All @@ -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())
}
Expand All @@ -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())
}
Expand All @@ -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())
}
Expand All @@ -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())
}
Expand All @@ -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())
}
Expand All @@ -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())
}
Expand All @@ -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())
}
Expand All @@ -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())
}
Expand Down Expand Up @@ -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},
Expand Down
23 changes: 23 additions & 0 deletions internal/webserver/webserver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package webserver_test
import (
"log"
"net/http"
"net/url"
"strings"
"sync"
"testing"
"time"
Expand Down Expand Up @@ -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)
}

0 comments on commit d02ecbe

Please sign in to comment.