|
@ -3,6 +3,7 @@ package auth |
|
|
import ( |
|
|
import ( |
|
|
"encoding/json" |
|
|
"encoding/json" |
|
|
"net/http" |
|
|
"net/http" |
|
|
|
|
|
"net/http/cookiejar" |
|
|
"net/http/httptest" |
|
|
"net/http/httptest" |
|
|
"net/url" |
|
|
"net/url" |
|
|
"strings" |
|
|
"strings" |
|
@ -22,8 +23,16 @@ func (hs *handlerStruct) ServeHTTP(w http.ResponseWriter, req *http.Request) { |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
func TestHandler(t *testing.T) { |
|
|
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"} |
|
|
auther := testAuther{FullName: "Test"} |
|
|
Register(&auther) |
|
|
Register(&auther) |
|
|
|
|
|
|
|
@ -41,7 +50,7 @@ func TestHandler(t *testing.T) { |
|
|
form3.Set("password", "stuff'nthings") |
|
|
form3.Set("password", "stuff'nthings") |
|
|
|
|
|
|
|
|
t.Run("Register", func(t *testing.T) { |
|
|
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 { |
|
|
if err != nil { |
|
|
t.Error("Request:", err) |
|
|
t.Error("Request:", err) |
|
|
t.Fail() |
|
|
t.Fail() |
|
@ -67,7 +76,7 @@ func TestHandler(t *testing.T) { |
|
|
}) |
|
|
}) |
|
|
|
|
|
|
|
|
t.Run("Login", func(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 { |
|
|
if err != nil { |
|
|
t.Error("Request:", err) |
|
|
t.Error("Request:", err) |
|
|
t.Fail() |
|
|
t.Fail() |
|
@ -78,6 +87,11 @@ func TestHandler(t *testing.T) { |
|
|
t.Fail() |
|
|
t.Fail() |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
if len(resp.Cookies()) == 0 || len(client.Jar.Cookies(url2)) == 0 { |
|
|
|
|
|
t.Error("No cookies set") |
|
|
|
|
|
t.Fail() |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
respSession := Session{} |
|
|
respSession := Session{} |
|
|
json.NewDecoder(resp.Body).Decode(&respSession) |
|
|
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) { |
|
|
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 { |
|
|
if err != nil { |
|
|
t.Error("Request:", err) |
|
|
t.Error("Request:", err) |
|
|
t.Fail() |
|
|
t.Fail() |
|
|