|
| 1 | +package flagship |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "net/http" |
| 6 | + "strings" |
| 7 | + "testing" |
| 8 | + |
| 9 | + "github.com/cssbruno/gowdk/runtime/form" |
| 10 | + "github.com/cssbruno/gowdk/runtime/response" |
| 11 | +) |
| 12 | + |
| 13 | +func TestLoginRequiresExplicitSessionSecret(t *testing.T) { |
| 14 | + resetTestSessions(t) |
| 15 | + t.Setenv("GOWDK_FLAGSHIP_EMAIL", "demo@example.com") |
| 16 | + t.Setenv("GOWDK_FLAGSHIP_SECRET", "") |
| 17 | + t.Setenv("GOWDK_FLAGSHIP_PASSWORD", "demo-password") |
| 18 | + |
| 19 | + result := loginForTest(t, "demo@example.com", "demo-password") |
| 20 | + |
| 21 | + assertLoginFailed(t, result) |
| 22 | +} |
| 23 | + |
| 24 | +func TestLoginRequiresExplicitPassword(t *testing.T) { |
| 25 | + resetTestSessions(t) |
| 26 | + t.Setenv("GOWDK_FLAGSHIP_EMAIL", "demo@example.com") |
| 27 | + t.Setenv("GOWDK_FLAGSHIP_SECRET", "development-flagship-session-secret-32b") |
| 28 | + t.Setenv("GOWDK_FLAGSHIP_PASSWORD", "") |
| 29 | + |
| 30 | + result := loginForTest(t, "demo@example.com", "demo-password") |
| 31 | + |
| 32 | + assertLoginFailed(t, result) |
| 33 | +} |
| 34 | + |
| 35 | +func TestLoginCreatesSignedSessionWithExplicitCredentials(t *testing.T) { |
| 36 | + resetTestSessions(t) |
| 37 | + t.Setenv("GOWDK_FLAGSHIP_EMAIL", "demo@example.com") |
| 38 | + t.Setenv("GOWDK_FLAGSHIP_SECRET", "development-flagship-session-secret-32b") |
| 39 | + t.Setenv("GOWDK_FLAGSHIP_PASSWORD", "demo-password") |
| 40 | + |
| 41 | + result := loginForTest(t, "demo@example.com", "demo-password") |
| 42 | + |
| 43 | + if result.Kind != response.Redirect || result.URL != "/dashboard" { |
| 44 | + t.Fatalf("login result = %#v, want redirect to dashboard", result) |
| 45 | + } |
| 46 | + if len(result.Cookies) != 1 { |
| 47 | + t.Fatalf("cookies = %#v, want one session cookie", result.Cookies) |
| 48 | + } |
| 49 | + cookie := result.Cookies[0] |
| 50 | + if cookie.Name != sessionCookie || !cookie.HttpOnly || cookie.Value == "" || !strings.Contains(cookie.Value, ".") { |
| 51 | + t.Fatalf("session cookie = %#v", cookie) |
| 52 | + } |
| 53 | + |
| 54 | + request, err := http.NewRequest(http.MethodGet, "/dashboard", nil) |
| 55 | + if err != nil { |
| 56 | + t.Fatal(err) |
| 57 | + } |
| 58 | + request.AddCookie(&cookie) |
| 59 | + if current, ok := currentSession(request); !ok || current.Email != "demo@example.com" { |
| 60 | + t.Fatalf("current session = %#v ok=%v", current, ok) |
| 61 | + } |
| 62 | +} |
| 63 | + |
| 64 | +func loginForTest(t *testing.T, email string, password string) response.Response { |
| 65 | + t.Helper() |
| 66 | + result, err := Login(context.Background(), form.Values{ |
| 67 | + "email": {email}, |
| 68 | + "password": {password}, |
| 69 | + }) |
| 70 | + if err != nil { |
| 71 | + t.Fatalf("Login returned error: %v", err) |
| 72 | + } |
| 73 | + return result |
| 74 | +} |
| 75 | + |
| 76 | +func assertLoginFailed(t *testing.T, result response.Response) { |
| 77 | + t.Helper() |
| 78 | + if result.Kind != response.Redirect || result.URL != "/?login=failed" { |
| 79 | + t.Fatalf("login result = %#v, want failed redirect", result) |
| 80 | + } |
| 81 | + if len(result.Cookies) != 0 { |
| 82 | + t.Fatalf("failed login set cookies: %#v", result.Cookies) |
| 83 | + } |
| 84 | +} |
| 85 | + |
| 86 | +func resetTestSessions(t *testing.T) { |
| 87 | + t.Helper() |
| 88 | + sessions.Lock() |
| 89 | + defer sessions.Unlock() |
| 90 | + sessions.Values = map[string]session{} |
| 91 | +} |
0 commit comments