|
|
@ -3,6 +3,7 @@ package auth |
|
|
|
import ( |
|
|
|
"encoding/json" |
|
|
|
"net/http" |
|
|
|
"net/http/cookiejar" |
|
|
|
"net/http/httptest" |
|
|
|
"net/url" |
|
|
|
"strings" |
|
|
@ -22,8 +23,16 @@ func (hs *handlerStruct) ServeHTTP(w http.ResponseWriter, req *http.Request) { |
|
|
|
} |
|
|
|
|
|
|
|
func TestHandler(t *testing.T) { |
|
|
|
server := httptest.NewServer(&handlerStruct{}) |
|
|
|
cookieJar, err := cookiejar.New(nil) |
|
|
|
if err != nil { |
|
|
|
t.Error("Cookie Jar:", err) |
|
|
|
t.Fail() |
|
|
|
return |
|
|
|
} |
|
|
|
|
|
|
|
server := httptest.NewServer(&handlerStruct{}) |
|
|
|
url2, _ := url.Parse(server.URL) |
|
|
|
client := &http.Client{Jar: cookieJar} |
|
|
|
auther := testAuther{FullName: "Test"} |
|
|
|
Register(&auther) |
|
|
|
|
|
|
@ -41,7 +50,7 @@ func TestHandler(t *testing.T) { |
|
|
|
form3.Set("password", "stuff'nthings") |
|
|
|
|
|
|
|
t.Run("Register", func(t *testing.T) { |
|
|
|
resp, err := http.PostForm(server.URL+"/auth/register", form) |
|
|
|
resp, err := client.PostForm(server.URL+"/auth/register", form) |
|
|
|
if err != nil { |
|
|
|
t.Error("Request:", err) |
|
|
|
t.Fail() |
|
|
@ -67,7 +76,7 @@ func TestHandler(t *testing.T) { |
|
|
|
}) |
|
|
|
|
|
|
|
t.Run("Login", func(t *testing.T) { |
|
|
|
resp, err := http.PostForm(server.URL+"/auth/login", form) |
|
|
|
resp, err := client.PostForm(server.URL+"/auth/login", form) |
|
|
|
if err != nil { |
|
|
|
t.Error("Request:", err) |
|
|
|
t.Fail() |
|
|
@ -78,6 +87,11 @@ func TestHandler(t *testing.T) { |
|
|
|
t.Fail() |
|
|
|
} |
|
|
|
|
|
|
|
if len(resp.Cookies()) == 0 || len(client.Jar.Cookies(url2)) == 0 { |
|
|
|
t.Error("No cookies set") |
|
|
|
t.Fail() |
|
|
|
} |
|
|
|
|
|
|
|
respSession := Session{} |
|
|
|
json.NewDecoder(resp.Body).Decode(&respSession) |
|
|
|
|
|
|
@ -87,8 +101,30 @@ func TestHandler(t *testing.T) { |
|
|
|
} |
|
|
|
}) |
|
|
|
|
|
|
|
// TODO: Move to router test
|
|
|
|
/* t.Run("Status", func(t *testing.T) { |
|
|
|
resp, err := client.Get(server.URL + "/auth/status?method=test") |
|
|
|
if err != nil { |
|
|
|
t.Error("Request:", err) |
|
|
|
t.Fail() |
|
|
|
} |
|
|
|
|
|
|
|
if resp.StatusCode != 200 { |
|
|
|
t.Error("Expected 200, got", resp.Status) |
|
|
|
t.Fail() |
|
|
|
} |
|
|
|
|
|
|
|
respSession := Session{} |
|
|
|
json.NewDecoder(resp.Body).Decode(&respSession) |
|
|
|
|
|
|
|
if respSession.UserID == "" { |
|
|
|
t.Errorf("No user ID in session") |
|
|
|
t.Fail() |
|
|
|
} |
|
|
|
}) */ |
|
|
|
|
|
|
|
t.Run("Login_Fail", func(t *testing.T) { |
|
|
|
resp, err := http.PostForm(server.URL+"/auth/login", form3) |
|
|
|
resp, err := client.PostForm(server.URL+"/auth/login", form3) |
|
|
|
if err != nil { |
|
|
|
t.Error("Request:", err) |
|
|
|
t.Fail() |
|
|
|