hatecomputers.club/api/auth/auth_test.go

99 lines
2.4 KiB
Go
Raw Normal View History

2024-04-03 19:53:50 -04:00
package auth_test
import (
"database/sql"
2024-04-04 18:03:34 -04:00
"net/http"
"net/http/httptest"
"os"
2024-04-04 18:03:34 -04:00
"testing"
2024-04-04 18:03:34 -04:00
"git.hatecomputers.club/hatecomputers/hatecomputers.club/api/auth"
2024-04-03 19:53:50 -04:00
"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"
)
2024-04-04 18:03:34 -04:00
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)
}
}
2024-04-03 19:53:50 -04:00
func setup() (*sql.DB, *types.RequestContext, func()) {
randomDb := utils.RandomId()
testDb := database.MakeConn(&randomDb)
database.Migrate(testDb)
2024-04-03 19:53:50 -04:00
context := &types.RequestContext{
DBConn: testDb,
Args: &args.Arguments{},
TemplateData: &(map[string]interface{}{}),
}
return testDb, context, func() {
testDb.Close()
os.Remove(randomDb)
}
}
2024-04-04 18:03:34 -04:00
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) {
}