summaryrefslogtreecommitdiff
path: root/api/auth/auth_test.go
diff options
context:
space:
mode:
authorElizabeth <elizabeth@simponic.xyz>2024-04-04 16:03:34 -0600
committerElizabeth <elizabeth@simponic.xyz>2024-04-04 16:03:34 -0600
commit94984aa4b01e96773b71325b5b27e6f64d9bd102 (patch)
tree515204ef0bdee10198bd4ec64972bf40f640a1c3 /api/auth/auth_test.go
parentc24e34ae856204f4299ddda26c5877a1abaf2e73 (diff)
downloadhatecomputers.club-94984aa4b01e96773b71325b5b27e6f64d9bd102.tar.gz
hatecomputers.club-94984aa4b01e96773b71325b5b27e6f64d9bd102.zip
auth test scaffolding
Diffstat (limited to 'api/auth/auth_test.go')
-rw-r--r--api/auth/auth_test.go74
1 files changed, 68 insertions, 6 deletions
diff --git a/api/auth/auth_test.go b/api/auth/auth_test.go
index a6c2a45..caaedf1 100644
--- a/api/auth/auth_test.go
+++ b/api/auth/auth_test.go
@@ -2,14 +2,24 @@ package auth_test
import (
"database/sql"
+ "net/http"
+ "net/http/httptest"
"os"
+ "testing"
+ "git.hatecomputers.club/hatecomputers/hatecomputers.club/api/auth"
"git.hatecomputers.club/hatecomputers/hatecomputers.club/api/types"
"git.hatecomputers.club/hatecomputers/hatecomputers.club/args"
"git.hatecomputers.club/hatecomputers/hatecomputers.club/database"
"git.hatecomputers.club/hatecomputers/hatecomputers.club/utils"
)
+func IdContinuation(context *types.RequestContext, req *http.Request, resp http.ResponseWriter) types.ContinuationChain {
+ return func(success types.Continuation, _failure types.Continuation) types.ContinuationChain {
+ return success(context, req, resp)
+ }
+}
+
func setup() (*sql.DB, *types.RequestContext, func()) {
randomDb := utils.RandomId()
@@ -28,9 +38,61 @@ func setup() (*sql.DB, *types.RequestContext, func()) {
}
}
-/*
-todo: test types key creation
-+ api key attached to user
-+ user session is unique
-+ goLogin goes to page in cookie
-*/
+func TestLoginSendsYouToRedirect(t *testing.T) {
+ db, context, cleanup := setup()
+ defer cleanup()
+
+ user := &database.User{
+ ID: "test",
+ Username: "test",
+ }
+ database.FindOrSaveUser(db, user)
+
+ session, _ := database.MakeUserSessionFor(db, user)
+
+ testServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
+ auth.VerifySessionContinuation(context, r, w)(IdContinuation, auth.GoLoginContinuation)(IdContinuation, IdContinuation)
+ }))
+ defer testServer.Close()
+
+ protectedPath := testServer.URL + "/protected-path"
+ req := httptest.NewRequest("GET", protectedPath, nil)
+ resp := httptest.NewRecorder()
+ testServer.Config.Handler.ServeHTTP(resp, req)
+
+ location := resp.Header().Get("Location")
+ if resp.Code != http.StatusFound && location != "/login" {
+ t.Errorf("expected redirect code, got %d, to login, got %s", resp.Code, location)
+ }
+
+ req.AddCookie(&http.Cookie{
+ Name: "session",
+ Value: session.ID,
+ MaxAge: 60,
+ })
+ resp = httptest.NewRecorder()
+ testServer.Config.Handler.ServeHTTP(resp, req)
+ if resp.Code != http.StatusOK {
+}
+
+func TestOauthFormatsUsername(t *testing.T) {
+
+}
+
+func TestSessionIsUnique(t *testing.T) {}
+
+func TestLogoutClearsCookie(t *testing.T) {
+
+}
+
+func TestRefreshUpdatesExpiration(t *testing.T) {
+
+}
+
+func TestVerifySessionEnsuresNonExpired(t *testing.T) {
+
+}
+
+func TestAPITokensAreEquivalentToSessions(t *testing.T) {
+
+}