Skip to content

Commit

Permalink
auth routes migrated over to echo
Browse files Browse the repository at this point in the history
  • Loading branch information
karngyan committed Dec 23, 2024
1 parent 4526925 commit 6783650
Show file tree
Hide file tree
Showing 26 changed files with 226 additions and 325 deletions.
3 changes: 3 additions & 0 deletions domains/auth/find.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,9 @@ func FetchWorkspaceByID(ctx context.Context, id int64) (*Workspace, error) {
return WorkspaceFromDB(&dw), nil
}

// FetchWorkspacesForUser fetches all workspaces for a user
// if no workspaces are found, it returns an empty slice
// if an error occurs with db query, it returns the error
func FetchWorkspacesForUser(ctx context.Context, userID int64) ([]*Workspace, error) {
dws, err := db.Q.GetWorkspacesForUser(ctx, userID)
if err != nil && !errors.Is(err, pgx.ErrNoRows) {
Expand Down
29 changes: 0 additions & 29 deletions routers/auth/dom.go

This file was deleted.

16 changes: 0 additions & 16 deletions routers/auth/info.go

This file was deleted.

66 changes: 0 additions & 66 deletions routers/auth/login.go

This file was deleted.

64 changes: 0 additions & 64 deletions routers/auth/login_test.go

This file was deleted.

15 changes: 0 additions & 15 deletions routers/auth/router.go

This file was deleted.

28 changes: 0 additions & 28 deletions routers/auth/setup_test.go

This file was deleted.

21 changes: 0 additions & 21 deletions routers/auth/testdata/login_test.TestLogin.approved.json

This file was deleted.

This file was deleted.

This file was deleted.

21 changes: 0 additions & 21 deletions routers/auth/testdata/register_test.TestRegister.approved.json

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

2 changes: 0 additions & 2 deletions routers/routers.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,11 @@ package routers

import (
"github.com/beego/beego/v2/core/logs"
"github.com/karngyan/maek/routers/auth"
"github.com/karngyan/maek/routers/collections"
"github.com/karngyan/maek/routers/notes"
)

func Init(l *logs.BeeLogger) error {
auth.Configure(l)
notes.Configure(l)
collections.Configure(l)
return nil
Expand Down
17 changes: 17 additions & 0 deletions ui_api/auth/info.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package auth

import (
"net/http"

"github.com/karngyan/maek/domains/auth"
"github.com/karngyan/maek/ui_api/models"
"github.com/karngyan/maek/ui_api/web"
)

func info(ctx web.Context) error {
return ctx.JSON(http.StatusOK, models.ModelForAuthBundle(&auth.Bundle{
User: ctx.User,
Session: ctx.Session,
Workspaces: ctx.AllWorkspaces,
}))
}
39 changes: 39 additions & 0 deletions ui_api/auth/login_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,42 @@ func TestLogin(t *testing.T) {
approvals.VerifyJSONBytes(t, rr.Body.Bytes())
assert.Contains(t, rr.Header().Get("Set-Cookie"), "HttpOnly; Secure; SameSite=Strict")
}

func TestLoginErrors(t *testing.T) {
defer testutil.TruncateTables()
cs := testutil.NewClientStateWithUser(t)

type testCase struct {
name string
body map[string]any
expectedCode int
}

tcs := []testCase{
{
name: "Invalid email",
body: map[string]any{
"email": "wrong-email",
"password": "test-password",
},
expectedCode: 400,
},
{
name: "Invalid password",
body: map[string]any{
"email": "[email protected]",
"password": "wrong-password",
},
expectedCode: 400,
},
}

for _, tc := range tcs {
t.Run(tc.name, func(t *testing.T) {
rr, err := cs.Post("/v1/auth/login", tc.body)
assert.Nil(t, err)
assert.Equal(t, tc.expectedCode, rr.Code)
approvals.VerifyJSONBytes(t, rr.Body.Bytes())
})
}
}
Loading

0 comments on commit 6783650

Please sign in to comment.