Skip to content

Commit

Permalink
tdd(authguard): add tests
Browse files Browse the repository at this point in the history
fixes: $129
  • Loading branch information
Jaeiya committed Jun 4, 2024
1 parent af220e0 commit 16bf817
Showing 1 changed file with 88 additions and 0 deletions.
88 changes: 88 additions & 0 deletions internal/middleware/auth_guard_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
package middleware

import (
"net/http"
"testing"

"github.com/Everything-Explained/go-server/internal/db"
"github.com/Everything-Explained/go-server/internal/router"
"github.com/Everything-Explained/go-server/testutils"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func TestAuthGuard(t *testing.T) {
t.Parallel()
a := assert.New(t)
rq := require.New(t)

tmpDir := t.TempDir()

u, err := db.NewUsers(tmpDir)
rq.NoError(err, "should initialize new users")

defer u.Close()
u.Add(false)

Check failure on line 25 in internal/middleware/auth_guard_test.go

View workflow job for this annotation

GitHub Actions / lint

Error return value of `u.Add` is not checked (errcheck)
userID, err := u.GetRandomUserId()
rq.NoError(err, "should get random user id")

r := router.NewRouter()
r.Get("/", func(w http.ResponseWriter, r *http.Request) {

Check warning on line 30 in internal/middleware/auth_guard_test.go

View workflow job for this annotation

GitHub Actions / lint

unused-parameter: parameter 'r' seems to be unused, consider removing or renaming it to match ^_ (revive)
w.WriteHeader(200)

Check failure on line 31 in internal/middleware/auth_guard_test.go

View workflow job for this annotation

GitHub Actions / lint

"200" can be replaced by http.StatusOK (usestdlibvars)
}, AuthGuard(u))

t.Run("halts req chain on bad authorization header", func(*testing.T) {
req := testutils.MockRequest(
r.Handler,
"GET",
"/",
nil,
nil, // test no header
)

a.Equal(http.StatusUnauthorized, req.Code, "should return unauthorized status")
a.Equal("Malformed Authorization\n", req.Body.String(), "should return reason")

req = testutils.MockRequest(
r.Handler,
"GET",
"/",
nil,
&map[string][]string{
"Authorization": {"Bearer "}, // test invalid header
},
)

a.Equal(http.StatusUnauthorized, req.Code, "should return unauthorized status")
a.Equal("Malformed Authorization\n", req.Body.String(), "should return reason")
})

t.Run("halts req chain on unauthorized users", func(*testing.T) {
req := testutils.MockRequest(
r.Handler,
"GET",
"/",
nil,
&map[string][]string{
"Authorization": {"Bearer testuser"},
},
)

a.Equal(http.StatusForbidden, req.Code, "should return forbidden status")
a.Equal("Bad User\n", req.Body.String(), "should return reason")
})

t.Run("resumes req chain on valid users", func(*testing.T) {
req := testutils.MockRequest(
r.Handler,
"GET",
"/",
nil,
&map[string][]string{
"Authorization": {"Bearer " + userID},
},
)

a.Equal(http.StatusOK, req.Code, "should return okay status")
})
}

0 comments on commit 16bf817

Please sign in to comment.