|
@ -2,85 +2,23 @@ package Auth_test |
|
|
|
|
|
|
|
|
import ( |
|
|
import ( |
|
|
"bytes" |
|
|
"bytes" |
|
|
"encoding/base64" |
|
|
|
|
|
"encoding/json" |
|
|
"encoding/json" |
|
|
"io/ioutil" |
|
|
|
|
|
"log" |
|
|
|
|
|
"net/http" |
|
|
"net/http" |
|
|
"net/http/cookiejar" |
|
|
|
|
|
"net/http/httptest" |
|
|
|
|
|
"net/url" |
|
|
|
|
|
"testing" |
|
|
"testing" |
|
|
"time" |
|
|
|
|
|
|
|
|
|
|
|
"git.tovijaeschke.xyz/tovi/Capsule/Backend/Api" |
|
|
|
|
|
"git.tovijaeschke.xyz/tovi/Capsule/Backend/Api/Auth" |
|
|
"git.tovijaeschke.xyz/tovi/Capsule/Backend/Api/Auth" |
|
|
"git.tovijaeschke.xyz/tovi/Capsule/Backend/Database" |
|
|
"git.tovijaeschke.xyz/tovi/Capsule/Backend/Database" |
|
|
"git.tovijaeschke.xyz/tovi/Capsule/Backend/Database/Seeder" |
|
|
|
|
|
"git.tovijaeschke.xyz/tovi/Capsule/Backend/Models" |
|
|
|
|
|
"github.com/gorilla/mux" |
|
|
|
|
|
|
|
|
"git.tovijaeschke.xyz/tovi/Capsule/Backend/Tests" |
|
|
) |
|
|
) |
|
|
|
|
|
|
|
|
func Test_ChangePassword(t *testing.T) { |
|
|
func Test_ChangePassword(t *testing.T) { |
|
|
log.SetOutput(ioutil.Discard) |
|
|
|
|
|
Database.InitTest() |
|
|
|
|
|
|
|
|
|
|
|
r := mux.NewRouter() |
|
|
|
|
|
Api.InitAPIEndpoints(r) |
|
|
|
|
|
ts := httptest.NewServer(r) |
|
|
|
|
|
|
|
|
client, ts, err := Tests.InitTestEnv() |
|
|
defer ts.Close() |
|
|
defer ts.Close() |
|
|
|
|
|
|
|
|
userKey, _ := Seeder.GenerateAesKey() |
|
|
|
|
|
pubKey := Seeder.GetPubKey() |
|
|
|
|
|
|
|
|
|
|
|
p, _ := Auth.HashPassword("password") |
|
|
|
|
|
|
|
|
|
|
|
u := Models.User{ |
|
|
|
|
|
Username: "test", |
|
|
|
|
|
Password: p, |
|
|
|
|
|
AsymmetricPublicKey: Seeder.PublicKey, |
|
|
|
|
|
AsymmetricPrivateKey: Seeder.EncryptedPrivateKey, |
|
|
|
|
|
SymmetricKey: base64.StdEncoding.EncodeToString( |
|
|
|
|
|
Seeder.EncryptWithPublicKey(userKey.Key, pubKey), |
|
|
|
|
|
), |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
err := Database.CreateUser(&u) |
|
|
|
|
|
if err != nil { |
|
|
if err != nil { |
|
|
t.Errorf("Expected nil, recieved %s", err.Error()) |
|
|
t.Errorf("Expected nil, recieved %s", err.Error()) |
|
|
return |
|
|
return |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
session := Models.Session{ |
|
|
|
|
|
UserID: u.ID, |
|
|
|
|
|
Expiry: time.Now().Add(12 * time.Hour), |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
err = Database.CreateSession(&session) |
|
|
|
|
|
if err != nil { |
|
|
|
|
|
t.Errorf("Expected nil, recieved %s", err.Error()) |
|
|
|
|
|
return |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
jar, err := cookiejar.New(nil) |
|
|
|
|
|
if err != nil { |
|
|
|
|
|
t.Errorf("Expected nil, recieved %s", err.Error()) |
|
|
|
|
|
return |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
url, _ := url.Parse(ts.URL) |
|
|
|
|
|
|
|
|
|
|
|
jar.SetCookies( |
|
|
|
|
|
url, |
|
|
|
|
|
[]*http.Cookie{ |
|
|
|
|
|
{ |
|
|
|
|
|
Name: "session_token", |
|
|
|
|
|
Value: session.ID.String(), |
|
|
|
|
|
MaxAge: 300, |
|
|
|
|
|
}, |
|
|
|
|
|
}, |
|
|
|
|
|
) |
|
|
|
|
|
|
|
|
|
|
|
d := struct { |
|
|
d := struct { |
|
|
OldPassword string `json:"old_password"` |
|
|
OldPassword string `json:"old_password"` |
|
|
NewPassword string `json:"new_password"` |
|
|
NewPassword string `json:"new_password"` |
|
@ -97,10 +35,6 @@ func Test_ChangePassword(t *testing.T) { |
|
|
req, _ := http.NewRequest("POST", ts.URL+"/api/v1/auth/change_password", bytes.NewBuffer(jsonStr)) |
|
|
req, _ := http.NewRequest("POST", ts.URL+"/api/v1/auth/change_password", bytes.NewBuffer(jsonStr)) |
|
|
req.Header.Set("Content-Type", "application/json") |
|
|
req.Header.Set("Content-Type", "application/json") |
|
|
|
|
|
|
|
|
client := &http.Client{ |
|
|
|
|
|
Jar: jar, |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
resp, err := client.Do(req) |
|
|
resp, err := client.Do(req) |
|
|
if err != nil { |
|
|
if err != nil { |
|
|
t.Errorf("Expected nil, recieved %s", err.Error()) |
|
|
t.Errorf("Expected nil, recieved %s", err.Error()) |
|
@ -112,7 +46,7 @@ func Test_ChangePassword(t *testing.T) { |
|
|
return |
|
|
return |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
u, err = Database.GetUserById(u.ID.String()) |
|
|
|
|
|
|
|
|
u, err := Database.GetUserByUsername("test") |
|
|
if err != nil { |
|
|
if err != nil { |
|
|
t.Errorf("Expected nil, recieved %s", err.Error()) |
|
|
t.Errorf("Expected nil, recieved %s", err.Error()) |
|
|
return |
|
|
return |
|
@ -124,65 +58,13 @@ func Test_ChangePassword(t *testing.T) { |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
func Test_ChangePasswordMismatchConfirmFails(t *testing.T) { |
|
|
func Test_ChangePasswordMismatchConfirmFails(t *testing.T) { |
|
|
log.SetOutput(ioutil.Discard) |
|
|
|
|
|
Database.InitTest() |
|
|
|
|
|
|
|
|
|
|
|
r := mux.NewRouter() |
|
|
|
|
|
Api.InitAPIEndpoints(r) |
|
|
|
|
|
ts := httptest.NewServer(r) |
|
|
|
|
|
|
|
|
client, ts, err := Tests.InitTestEnv() |
|
|
defer ts.Close() |
|
|
defer ts.Close() |
|
|
|
|
|
|
|
|
userKey, _ := Seeder.GenerateAesKey() |
|
|
|
|
|
pubKey := Seeder.GetPubKey() |
|
|
|
|
|
|
|
|
|
|
|
p, _ := Auth.HashPassword("password") |
|
|
|
|
|
|
|
|
|
|
|
u := Models.User{ |
|
|
|
|
|
Username: "test", |
|
|
|
|
|
Password: p, |
|
|
|
|
|
AsymmetricPublicKey: Seeder.PublicKey, |
|
|
|
|
|
AsymmetricPrivateKey: Seeder.EncryptedPrivateKey, |
|
|
|
|
|
SymmetricKey: base64.StdEncoding.EncodeToString( |
|
|
|
|
|
Seeder.EncryptWithPublicKey(userKey.Key, pubKey), |
|
|
|
|
|
), |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
err := Database.CreateUser(&u) |
|
|
|
|
|
if err != nil { |
|
|
|
|
|
t.Errorf("Expected nil, recieved %s", err.Error()) |
|
|
|
|
|
return |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
session := Models.Session{ |
|
|
|
|
|
UserID: u.ID, |
|
|
|
|
|
Expiry: time.Now().Add(12 * time.Hour), |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
err = Database.CreateSession(&session) |
|
|
|
|
|
if err != nil { |
|
|
if err != nil { |
|
|
t.Errorf("Expected nil, recieved %s", err.Error()) |
|
|
t.Errorf("Expected nil, recieved %s", err.Error()) |
|
|
return |
|
|
return |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
jar, err := cookiejar.New(nil) |
|
|
|
|
|
if err != nil { |
|
|
|
|
|
t.Errorf("Expected nil, recieved %s", err.Error()) |
|
|
|
|
|
return |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
url, _ := url.Parse(ts.URL) |
|
|
|
|
|
|
|
|
|
|
|
jar.SetCookies( |
|
|
|
|
|
url, |
|
|
|
|
|
[]*http.Cookie{ |
|
|
|
|
|
{ |
|
|
|
|
|
Name: "session_token", |
|
|
|
|
|
Value: session.ID.String(), |
|
|
|
|
|
MaxAge: 300, |
|
|
|
|
|
}, |
|
|
|
|
|
}, |
|
|
|
|
|
) |
|
|
|
|
|
|
|
|
|
|
|
d := struct { |
|
|
d := struct { |
|
|
OldPassword string `json:"old_password"` |
|
|
OldPassword string `json:"old_password"` |
|
|
NewPassword string `json:"new_password"` |
|
|
NewPassword string `json:"new_password"` |
|
@ -199,10 +81,6 @@ func Test_ChangePasswordMismatchConfirmFails(t *testing.T) { |
|
|
req, _ := http.NewRequest("POST", ts.URL+"/api/v1/auth/change_password", bytes.NewBuffer(jsonStr)) |
|
|
req, _ := http.NewRequest("POST", ts.URL+"/api/v1/auth/change_password", bytes.NewBuffer(jsonStr)) |
|
|
req.Header.Set("Content-Type", "application/json") |
|
|
req.Header.Set("Content-Type", "application/json") |
|
|
|
|
|
|
|
|
client := &http.Client{ |
|
|
|
|
|
Jar: jar, |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
resp, err := client.Do(req) |
|
|
resp, err := client.Do(req) |
|
|
if err != nil { |
|
|
if err != nil { |
|
|
t.Errorf("Expected nil, recieved %s", err.Error()) |
|
|
t.Errorf("Expected nil, recieved %s", err.Error()) |
|
@ -215,65 +93,13 @@ func Test_ChangePasswordMismatchConfirmFails(t *testing.T) { |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
func Test_ChangePasswordInvalidCurrentPasswordFails(t *testing.T) { |
|
|
func Test_ChangePasswordInvalidCurrentPasswordFails(t *testing.T) { |
|
|
log.SetOutput(ioutil.Discard) |
|
|
|
|
|
Database.InitTest() |
|
|
|
|
|
|
|
|
|
|
|
r := mux.NewRouter() |
|
|
|
|
|
Api.InitAPIEndpoints(r) |
|
|
|
|
|
ts := httptest.NewServer(r) |
|
|
|
|
|
|
|
|
client, ts, err := Tests.InitTestEnv() |
|
|
defer ts.Close() |
|
|
defer ts.Close() |
|
|
|
|
|
|
|
|
userKey, _ := Seeder.GenerateAesKey() |
|
|
|
|
|
pubKey := Seeder.GetPubKey() |
|
|
|
|
|
|
|
|
|
|
|
p, _ := Auth.HashPassword("password") |
|
|
|
|
|
|
|
|
|
|
|
u := Models.User{ |
|
|
|
|
|
Username: "test", |
|
|
|
|
|
Password: p, |
|
|
|
|
|
AsymmetricPublicKey: Seeder.PublicKey, |
|
|
|
|
|
AsymmetricPrivateKey: Seeder.EncryptedPrivateKey, |
|
|
|
|
|
SymmetricKey: base64.StdEncoding.EncodeToString( |
|
|
|
|
|
Seeder.EncryptWithPublicKey(userKey.Key, pubKey), |
|
|
|
|
|
), |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
err := Database.CreateUser(&u) |
|
|
|
|
|
if err != nil { |
|
|
if err != nil { |
|
|
t.Errorf("Expected nil, recieved %s", err.Error()) |
|
|
t.Errorf("Expected nil, recieved %s", err.Error()) |
|
|
return |
|
|
return |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
session := Models.Session{ |
|
|
|
|
|
UserID: u.ID, |
|
|
|
|
|
Expiry: time.Now().Add(12 * time.Hour), |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
err = Database.CreateSession(&session) |
|
|
|
|
|
if err != nil { |
|
|
|
|
|
t.Errorf("Expected nil, recieved %s", err.Error()) |
|
|
|
|
|
return |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
jar, err := cookiejar.New(nil) |
|
|
|
|
|
if err != nil { |
|
|
|
|
|
t.Errorf("Expected nil, recieved %s", err.Error()) |
|
|
|
|
|
return |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
url, _ := url.Parse(ts.URL) |
|
|
|
|
|
|
|
|
|
|
|
jar.SetCookies( |
|
|
|
|
|
url, |
|
|
|
|
|
[]*http.Cookie{ |
|
|
|
|
|
{ |
|
|
|
|
|
Name: "session_token", |
|
|
|
|
|
Value: session.ID.String(), |
|
|
|
|
|
MaxAge: 300, |
|
|
|
|
|
}, |
|
|
|
|
|
}, |
|
|
|
|
|
) |
|
|
|
|
|
|
|
|
|
|
|
d := struct { |
|
|
d := struct { |
|
|
OldPassword string `json:"old_password"` |
|
|
OldPassword string `json:"old_password"` |
|
|
NewPassword string `json:"new_password"` |
|
|
NewPassword string `json:"new_password"` |
|
@ -290,10 +116,6 @@ func Test_ChangePasswordInvalidCurrentPasswordFails(t *testing.T) { |
|
|
req, _ := http.NewRequest("POST", ts.URL+"/api/v1/auth/change_password", bytes.NewBuffer(jsonStr)) |
|
|
req, _ := http.NewRequest("POST", ts.URL+"/api/v1/auth/change_password", bytes.NewBuffer(jsonStr)) |
|
|
req.Header.Set("Content-Type", "application/json") |
|
|
req.Header.Set("Content-Type", "application/json") |
|
|
|
|
|
|
|
|
client := &http.Client{ |
|
|
|
|
|
Jar: jar, |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
resp, err := client.Do(req) |
|
|
resp, err := client.Do(req) |
|
|
if err != nil { |
|
|
if err != nil { |
|
|
t.Errorf("Expected nil, recieved %s", err.Error()) |
|
|
t.Errorf("Expected nil, recieved %s", err.Error()) |
|
|