package Auth_test
|
|
|
|
import (
|
|
"bytes"
|
|
"encoding/json"
|
|
"net/http"
|
|
"testing"
|
|
|
|
"git.tovijaeschke.xyz/tovi/Envelope/Backend/Database"
|
|
"git.tovijaeschke.xyz/tovi/Envelope/Backend/Tests"
|
|
)
|
|
|
|
func Test_Login(t *testing.T) {
|
|
_, ts, err := Tests.InitTestEnv()
|
|
defer ts.Close()
|
|
if err != nil {
|
|
t.Errorf("Expected nil, recieved %s", err.Error())
|
|
return
|
|
}
|
|
|
|
d := struct {
|
|
Username string `json:"username"`
|
|
Password string `json:"password"`
|
|
}{
|
|
Username: "test",
|
|
Password: "password",
|
|
}
|
|
|
|
jsonStr, _ := json.Marshal(d)
|
|
req, _ := http.NewRequest("POST", ts.URL+"/api/v1/login", bytes.NewBuffer(jsonStr))
|
|
req.Header.Set("Content-Type", "application/json")
|
|
|
|
client := &http.Client{}
|
|
resp, err := client.Do(req)
|
|
|
|
if err != nil {
|
|
t.Errorf("Expected nil, recieved %s", err.Error())
|
|
return
|
|
}
|
|
|
|
if resp.StatusCode != http.StatusOK {
|
|
t.Errorf("Expected %d, recieved %d", http.StatusOK, resp.StatusCode)
|
|
return
|
|
}
|
|
|
|
u, err := Database.GetUserByUsername("test")
|
|
if err != nil {
|
|
t.Errorf("Expected nil, recieved %s", err.Error())
|
|
return
|
|
}
|
|
|
|
var session Database.Session
|
|
|
|
err = Database.DB.First(&session, "user_id = ?", u.ID.String()).Error
|
|
|
|
if err != nil {
|
|
t.Errorf("Expected user record, recieved %s", err.Error())
|
|
return
|
|
}
|
|
}
|
|
|
|
func Test_Login_PasswordFails(t *testing.T) {
|
|
_, ts, err := Tests.InitTestEnv()
|
|
defer ts.Close()
|
|
if err != nil {
|
|
t.Errorf("Expected nil, recieved %s", err.Error())
|
|
return
|
|
}
|
|
|
|
d := struct {
|
|
Username string `json:"username"`
|
|
Password string `json:"password"`
|
|
}{
|
|
Username: "test",
|
|
Password: "password1",
|
|
}
|
|
|
|
jsonStr, _ := json.Marshal(d)
|
|
req, _ := http.NewRequest("POST", ts.URL+"/api/v1/login", bytes.NewBuffer(jsonStr))
|
|
req.Header.Set("Content-Type", "application/json")
|
|
|
|
client := &http.Client{}
|
|
resp, err := client.Do(req)
|
|
|
|
if err != nil {
|
|
t.Errorf("Expected nil, recieved %s", err.Error())
|
|
return
|
|
}
|
|
|
|
if resp.StatusCode != http.StatusUnauthorized {
|
|
t.Errorf("Expected %d, recieved %d", http.StatusUnauthorized, resp.StatusCode)
|
|
return
|
|
}
|
|
}
|