mirror of
https://github.com/volatiletech/authboss.git
synced 2024-11-28 08:58:38 +02:00
de1c2ed081
- Change changelog format to use keepachangelog standard - Refactor the config to be made of substructs to help organize all the pieces - Add the new interfaces to the configuration - Clean up module loading (no unnecessary reflection to create new value) - Change User interface to have a Get/SetPID not E-mail/Username, this way we don't ever have to refer to one or the other, we just always assume pid. In the case of Confirm/Recover we'll have to make a GetEmail or there won't be a way for us to get the e-mail to send to. - Delete the xsrf nonsense in the core
194 lines
4.4 KiB
Go
194 lines
4.4 KiB
Go
package authboss
|
|
|
|
import (
|
|
"io"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"strings"
|
|
"testing"
|
|
)
|
|
|
|
func TestStateGet(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
ab := New()
|
|
ab.Storage.SessionState = newMockClientStateRW("one", "two")
|
|
ab.Storage.CookieState = newMockClientStateRW("three", "four")
|
|
|
|
r := httptest.NewRequest("GET", "/", nil)
|
|
w := ab.NewResponse(httptest.NewRecorder(), r)
|
|
|
|
var err error
|
|
r, err = ab.LoadClientState(w, r)
|
|
if err != nil {
|
|
t.Error(err)
|
|
}
|
|
|
|
if got, _ := GetSession(r, "one"); got != "two" {
|
|
t.Error("session value was wrong:", got)
|
|
}
|
|
if got, _ := GetCookie(r, "three"); got != "four" {
|
|
t.Error("cookie value was wrong:", got)
|
|
}
|
|
}
|
|
|
|
func TestStateResponseWriterDoubleWritePanic(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
ab := New()
|
|
ab.Storage.SessionState = newMockClientStateRW("one", "two")
|
|
|
|
r := httptest.NewRequest("GET", "/", nil)
|
|
w := ab.NewResponse(httptest.NewRecorder(), r)
|
|
|
|
w.WriteHeader(200)
|
|
// Check this doesn't panic
|
|
w.WriteHeader(200)
|
|
|
|
defer func() {
|
|
if recover() == nil {
|
|
t.Error("expected a panic")
|
|
}
|
|
}()
|
|
|
|
w.putClientState()
|
|
}
|
|
|
|
func TestStateResponseWriterLastSecondWriteWithPrevious(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
ab := New()
|
|
ab.Storage.SessionState = newMockClientStateRW("one", "two")
|
|
ab.Storage.CookieState = newMockClientStateRW("three", "four")
|
|
|
|
r := httptest.NewRequest("GET", "/", nil)
|
|
var w http.ResponseWriter = httptest.NewRecorder()
|
|
|
|
var err error
|
|
r, err = ab.LoadClientState(w, r)
|
|
if err != nil {
|
|
t.Error(err)
|
|
}
|
|
w = ab.NewResponse(w, r)
|
|
|
|
w.WriteHeader(200)
|
|
|
|
// This is an odd test, since the mock will always overwrite the previous
|
|
// write with the cookie values. Keeping it anyway for code coverage
|
|
got := strings.TrimSpace(w.Header().Get("test_session"))
|
|
if got != `{"three":"four"}` {
|
|
t.Error("got:", got)
|
|
}
|
|
}
|
|
|
|
func TestStateResponseWriterLastSecondWriteHeader(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
ab := New()
|
|
ab.Storage.SessionState = newMockClientStateRW()
|
|
|
|
r := httptest.NewRequest("GET", "/", nil)
|
|
w := ab.NewResponse(httptest.NewRecorder(), r)
|
|
|
|
PutSession(w, "one", "two")
|
|
|
|
w.WriteHeader(200)
|
|
got := strings.TrimSpace(w.Header().Get("test_session"))
|
|
if got != `{"one":"two"}` {
|
|
t.Error("got:", got)
|
|
}
|
|
}
|
|
|
|
func TestStateResponseWriterLastSecondWriteWrite(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
ab := New()
|
|
ab.Storage.SessionState = newMockClientStateRW()
|
|
|
|
r := httptest.NewRequest("GET", "/", nil)
|
|
w := ab.NewResponse(httptest.NewRecorder(), r)
|
|
|
|
PutSession(w, "one", "two")
|
|
|
|
io.WriteString(w, "Hello world!")
|
|
|
|
got := strings.TrimSpace(w.Header().Get("test_session"))
|
|
if got != `{"one":"two"}` {
|
|
t.Error("got:", got)
|
|
}
|
|
}
|
|
|
|
func TestStateResponseWriterEvents(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
ab := New()
|
|
r := httptest.NewRequest("GET", "/", nil)
|
|
w := ab.NewResponse(httptest.NewRecorder(), r)
|
|
|
|
PutSession(w, "one", "two")
|
|
DelSession(w, "one")
|
|
DelCookie(w, "one")
|
|
PutCookie(w, "two", "one")
|
|
|
|
want := ClientStateEvent{Kind: ClientStateEventPut, Key: "one", Value: "two"}
|
|
if got := w.sessionStateEvents[0]; got != want {
|
|
t.Error("event was wrong", got)
|
|
}
|
|
|
|
want = ClientStateEvent{Kind: ClientStateEventDel, Key: "one"}
|
|
if got := w.sessionStateEvents[1]; got != want {
|
|
t.Error("event was wrong", got)
|
|
}
|
|
|
|
want = ClientStateEvent{Kind: ClientStateEventDel, Key: "one"}
|
|
if got := w.cookieStateEvents[0]; got != want {
|
|
t.Error("event was wrong", got)
|
|
}
|
|
|
|
want = ClientStateEvent{Kind: ClientStateEventPut, Key: "two", Value: "one"}
|
|
if got := w.cookieStateEvents[1]; got != want {
|
|
t.Error("event was wrong", got)
|
|
}
|
|
}
|
|
|
|
func TestFlashClearer(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
ab := New()
|
|
ab.Storage.SessionState = newMockClientStateRW(FlashSuccessKey, "a", FlashErrorKey, "b")
|
|
|
|
r := httptest.NewRequest("GET", "/", nil)
|
|
w := ab.NewResponse(httptest.NewRecorder(), r)
|
|
|
|
if msg := FlashSuccess(w, r); msg != "" {
|
|
t.Error("Unexpected flash success:", msg)
|
|
}
|
|
|
|
if msg := FlashError(w, r); msg != "" {
|
|
t.Error("Unexpected flash error:", msg)
|
|
}
|
|
|
|
var err error
|
|
r, err = ab.LoadClientState(w, r)
|
|
if err != nil {
|
|
t.Error(err)
|
|
}
|
|
|
|
if msg := FlashSuccess(w, r); msg != "a" {
|
|
t.Error("Unexpected flash success:", msg)
|
|
}
|
|
|
|
if msg := FlashError(w, r); msg != "b" {
|
|
t.Error("Unexpected flash error:", msg)
|
|
}
|
|
|
|
want := ClientStateEvent{Kind: ClientStateEventDel, Key: FlashSuccessKey}
|
|
if got := w.sessionStateEvents[0]; got != want {
|
|
t.Error("event was wrong", got)
|
|
}
|
|
want = ClientStateEvent{Kind: ClientStateEventDel, Key: FlashErrorKey}
|
|
if got := w.sessionStateEvents[1]; got != want {
|
|
t.Error("event was wrong", got)
|
|
}
|
|
}
|