|
@ -3,8 +3,6 @@ package Api |
|
|
import ( |
|
|
import ( |
|
|
"encoding/json" |
|
|
"encoding/json" |
|
|
"fmt" |
|
|
"fmt" |
|
|
"io/ioutil" |
|
|
|
|
|
"log" |
|
|
|
|
|
"math/rand" |
|
|
"math/rand" |
|
|
"net/http" |
|
|
"net/http" |
|
|
"net/http/httptest" |
|
|
"net/http/httptest" |
|
@ -13,11 +11,12 @@ import ( |
|
|
"runtime" |
|
|
"runtime" |
|
|
"strings" |
|
|
"strings" |
|
|
"testing" |
|
|
"testing" |
|
|
|
|
|
"time" |
|
|
|
|
|
|
|
|
|
|
|
"git.tovijaeschke.xyz/tovi/SuddenImpactRecords/Api/Auth" |
|
|
"git.tovijaeschke.xyz/tovi/SuddenImpactRecords/Database" |
|
|
"git.tovijaeschke.xyz/tovi/SuddenImpactRecords/Database" |
|
|
"git.tovijaeschke.xyz/tovi/SuddenImpactRecords/Models" |
|
|
"git.tovijaeschke.xyz/tovi/SuddenImpactRecords/Models" |
|
|
"github.com/gorilla/mux" |
|
|
"github.com/gorilla/mux" |
|
|
"gorm.io/gorm" |
|
|
|
|
|
) |
|
|
) |
|
|
|
|
|
|
|
|
func init() { |
|
|
func init() { |
|
@ -29,15 +28,14 @@ func init() { |
|
|
panic(err) |
|
|
panic(err) |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
log.SetOutput(ioutil.Discard) |
|
|
|
|
|
Database.Init() |
|
|
|
|
|
|
|
|
Database.InitTest() |
|
|
|
|
|
|
|
|
r = mux.NewRouter() |
|
|
r = mux.NewRouter() |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
var letterRunes = []rune("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ") |
|
|
var letterRunes = []rune("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ") |
|
|
|
|
|
|
|
|
func RandStringRunes(n int) string { |
|
|
|
|
|
|
|
|
func randString(n int) string { |
|
|
b := make([]rune, n) |
|
|
b := make([]rune, n) |
|
|
for i := range b { |
|
|
for i := range b { |
|
|
b[i] = letterRunes[rand.Intn(len(letterRunes))] |
|
|
b[i] = letterRunes[rand.Intn(len(letterRunes))] |
|
@ -45,6 +43,84 @@ func RandStringRunes(n int) string { |
|
|
return string(b) |
|
|
return string(b) |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
func createTestUser(random bool) (Models.User, error) { |
|
|
|
|
|
now := time.Now() |
|
|
|
|
|
|
|
|
|
|
|
email := "email@email.com" |
|
|
|
|
|
if random { |
|
|
|
|
|
email = fmt.Sprintf("%s@email.com", randString(16)) |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
password, err := Auth.HashPassword("password") |
|
|
|
|
|
if err != nil { |
|
|
|
|
|
return Models.User{}, err |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
userData := Models.User{ |
|
|
|
|
|
Email: email, |
|
|
|
|
|
Password: password, |
|
|
|
|
|
LastLogin: &now, |
|
|
|
|
|
FirstName: "Hugh", |
|
|
|
|
|
LastName: "Mann", |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
err = Database.CreateUser(&userData) |
|
|
|
|
|
return userData, err |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
func Test_getUser(t *testing.T) { |
|
|
|
|
|
t.Log("Testing getPost...") |
|
|
|
|
|
|
|
|
|
|
|
r.HandleFunc("/user/{userID}", getUser).Methods("GET") |
|
|
|
|
|
|
|
|
|
|
|
ts := httptest.NewServer(r) |
|
|
|
|
|
|
|
|
|
|
|
defer ts.Close() |
|
|
|
|
|
|
|
|
|
|
|
userData, err := createTestUser(false) |
|
|
|
|
|
if err != nil { |
|
|
|
|
|
t.Errorf("Expected nil, recieved %s", err.Error()) |
|
|
|
|
|
t.FailNow() |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
res, err := http.Get(fmt.Sprintf( |
|
|
|
|
|
"%s/user/%s", |
|
|
|
|
|
ts.URL, |
|
|
|
|
|
userData.ID, |
|
|
|
|
|
)) |
|
|
|
|
|
|
|
|
|
|
|
if err != nil { |
|
|
|
|
|
t.Errorf("Expected nil, recieved %s", err.Error()) |
|
|
|
|
|
t.FailNow() |
|
|
|
|
|
} |
|
|
|
|
|
if res.StatusCode != http.StatusOK { |
|
|
|
|
|
t.Errorf("Expected %d, recieved %d", http.StatusOK, res.StatusCode) |
|
|
|
|
|
t.FailNow() |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
getUserData := new(Models.User) |
|
|
|
|
|
err = json.NewDecoder(res.Body).Decode(getUserData) |
|
|
|
|
|
if err != nil { |
|
|
|
|
|
t.Errorf("Expected nil, recieved %s", err.Error()) |
|
|
|
|
|
t.FailNow() |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
if getUserData.Email != "email@email.com" { |
|
|
|
|
|
t.Errorf("Expected email \"email@email.com\", recieved %s", getUserData.Email) |
|
|
|
|
|
t.FailNow() |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
if getUserData.FirstName != "Hugh" { |
|
|
|
|
|
t.Errorf("Expected email \"Hugh\", recieved %s", getUserData.FirstName) |
|
|
|
|
|
t.FailNow() |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
if getUserData.LastName != "Mann" { |
|
|
|
|
|
t.Errorf("Expected email \"Mann\", recieved %s", getUserData.LastName) |
|
|
|
|
|
t.FailNow() |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
func Test_getUsers(t *testing.T) { |
|
|
func Test_getUsers(t *testing.T) { |
|
|
t.Log("Testing getUsers...") |
|
|
t.Log("Testing getUsers...") |
|
|
|
|
|
|
|
@ -55,43 +131,30 @@ func Test_getUsers(t *testing.T) { |
|
|
|
|
|
|
|
|
var err error |
|
|
var err error |
|
|
for i := 0; i < 20; i++ { |
|
|
for i := 0; i < 20; i++ { |
|
|
userData := Models.User{ |
|
|
|
|
|
Email: fmt.Sprintf( |
|
|
|
|
|
"%s@email.com", |
|
|
|
|
|
RandStringRunes(16), |
|
|
|
|
|
), |
|
|
|
|
|
Password: "password", |
|
|
|
|
|
ConfirmPassword: "password", |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
err = Database.CreateUser(&userData) |
|
|
|
|
|
if err != nil { |
|
|
|
|
|
t.Errorf("Expected nil, recieved %s", err.Error()) |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
defer Database.DB. |
|
|
|
|
|
Session(&gorm.Session{FullSaveAssociations: true}). |
|
|
|
|
|
Unscoped(). |
|
|
|
|
|
Delete(&userData) |
|
|
|
|
|
|
|
|
createTestUser(true) |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
res, err := http.Get(ts.URL + "/user?page=1&pageSize=10") |
|
|
res, err := http.Get(ts.URL + "/user?page=1&pageSize=10") |
|
|
|
|
|
|
|
|
if err != nil { |
|
|
if err != nil { |
|
|
t.Errorf("Expected nil, recieved %s", err.Error()) |
|
|
t.Errorf("Expected nil, recieved %s", err.Error()) |
|
|
|
|
|
t.FailNow() |
|
|
} |
|
|
} |
|
|
if res.StatusCode != http.StatusOK { |
|
|
if res.StatusCode != http.StatusOK { |
|
|
t.Errorf("Expected %d, recieved %d", http.StatusOK, res.StatusCode) |
|
|
t.Errorf("Expected %d, recieved %d", http.StatusOK, res.StatusCode) |
|
|
|
|
|
t.FailNow() |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
getUsersData := new([]Models.User) |
|
|
getUsersData := new([]Models.User) |
|
|
err = json.NewDecoder(res.Body).Decode(getUsersData) |
|
|
err = json.NewDecoder(res.Body).Decode(getUsersData) |
|
|
if err != nil { |
|
|
if err != nil { |
|
|
t.Errorf("Expected nil, recieved %s", err.Error()) |
|
|
t.Errorf("Expected nil, recieved %s", err.Error()) |
|
|
|
|
|
t.FailNow() |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
if len(*getUsersData) != 10 { |
|
|
if len(*getUsersData) != 10 { |
|
|
t.Errorf("Expected 10, recieved %d", len(*getUsersData)) |
|
|
t.Errorf("Expected 10, recieved %d", len(*getUsersData)) |
|
|
|
|
|
t.FailNow() |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
@ -104,15 +167,18 @@ func Test_createUser(t *testing.T) { |
|
|
|
|
|
|
|
|
defer ts.Close() |
|
|
defer ts.Close() |
|
|
|
|
|
|
|
|
|
|
|
email := fmt.Sprintf("%s@email.com", randString(16)) |
|
|
|
|
|
|
|
|
postJson := ` |
|
|
postJson := ` |
|
|
{ |
|
|
{ |
|
|
"email": "email@email.com", |
|
|
|
|
|
|
|
|
"email": "%s", |
|
|
"password": "password", |
|
|
"password": "password", |
|
|
"confirm_password": "password", |
|
|
"confirm_password": "password", |
|
|
"first_name": "Hugh", |
|
|
"first_name": "Hugh", |
|
|
"last_name": "Mann" |
|
|
"last_name": "Mann" |
|
|
} |
|
|
} |
|
|
` |
|
|
` |
|
|
|
|
|
postJson = fmt.Sprintf(postJson, email) |
|
|
|
|
|
|
|
|
res, err := http.Post(ts.URL+"/user", "application/json", strings.NewReader(postJson)) |
|
|
res, err := http.Post(ts.URL+"/user", "application/json", strings.NewReader(postJson)) |
|
|
if err != nil { |
|
|
if err != nil { |
|
@ -124,9 +190,107 @@ func Test_createUser(t *testing.T) { |
|
|
t.Errorf("Expected %d, recieved %d", http.StatusOK, res.StatusCode) |
|
|
t.Errorf("Expected %d, recieved %d", http.StatusOK, res.StatusCode) |
|
|
return |
|
|
return |
|
|
} |
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
func Test_updateUser(t *testing.T) { |
|
|
|
|
|
t.Log("Testing updateUser...") |
|
|
|
|
|
|
|
|
|
|
|
r.HandleFunc("/user/{userID}", updateUser).Methods("PUT") |
|
|
|
|
|
|
|
|
|
|
|
ts := httptest.NewServer(r) |
|
|
|
|
|
|
|
|
|
|
|
defer ts.Close() |
|
|
|
|
|
|
|
|
|
|
|
userData, err := createTestUser(true) |
|
|
|
|
|
if err != nil { |
|
|
|
|
|
t.Errorf("Expected nil, recieved %s", err.Error()) |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
email := fmt.Sprintf("%s@email.com", randString(16)) |
|
|
|
|
|
|
|
|
|
|
|
postJson := ` |
|
|
|
|
|
{ |
|
|
|
|
|
"email": "%s", |
|
|
|
|
|
"first_name": "first", |
|
|
|
|
|
"last_name": "last" |
|
|
|
|
|
} |
|
|
|
|
|
` |
|
|
|
|
|
postJson = fmt.Sprintf(postJson, email) |
|
|
|
|
|
|
|
|
|
|
|
req, err := http.NewRequest("PUT", fmt.Sprintf( |
|
|
|
|
|
"%s/user/%s", |
|
|
|
|
|
ts.URL, |
|
|
|
|
|
userData.ID, |
|
|
|
|
|
), strings.NewReader(postJson)) |
|
|
|
|
|
|
|
|
|
|
|
if err != nil { |
|
|
|
|
|
t.Errorf("Expected nil, recieved %s", err.Error()) |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// Fetch Request
|
|
|
|
|
|
res, err := http.DefaultClient.Do(req) |
|
|
|
|
|
if err != nil { |
|
|
|
|
|
t.Errorf("Expected nil, recieved %s", err.Error()) |
|
|
|
|
|
} |
|
|
|
|
|
defer res.Body.Close() |
|
|
|
|
|
|
|
|
|
|
|
if res.StatusCode != http.StatusOK { |
|
|
|
|
|
t.Errorf("Expected %d, recieved %d", http.StatusOK, res.StatusCode) |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
updateUserData := new(Models.User) |
|
|
|
|
|
err = json.NewDecoder(res.Body).Decode(updateUserData) |
|
|
|
|
|
if err != nil { |
|
|
|
|
|
t.Errorf("Expected nil, recieved %s", err.Error()) |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
if updateUserData.Email != email { |
|
|
|
|
|
t.Errorf("Expected email \"%s\", recieved %s", email, updateUserData.Email) |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
if updateUserData.FirstName != "first" { |
|
|
|
|
|
t.Errorf("Expected FirstName \"first\", recieved %s", updateUserData.FirstName) |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
if updateUserData.LastName != "last" { |
|
|
|
|
|
t.Errorf("Expected LastName \"last\", recieved %s", updateUserData.LastName) |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
func Test_deleteUser(t *testing.T) { |
|
|
|
|
|
t.Log("Testing deleteUser...") |
|
|
|
|
|
|
|
|
|
|
|
r.HandleFunc("/user/{userID}", deleteUser).Methods("DELETE") |
|
|
|
|
|
|
|
|
|
|
|
ts := httptest.NewServer(r) |
|
|
|
|
|
|
|
|
|
|
|
defer ts.Close() |
|
|
|
|
|
|
|
|
|
|
|
userData, err := createTestUser(true) |
|
|
|
|
|
if err != nil { |
|
|
|
|
|
t.Errorf("Expected nil, recieved %s", err.Error()) |
|
|
|
|
|
t.FailNow() |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
req, err := http.NewRequest("DELETE", fmt.Sprintf( |
|
|
|
|
|
"%s/user/%s", |
|
|
|
|
|
ts.URL, |
|
|
|
|
|
userData.ID, |
|
|
|
|
|
), nil) |
|
|
|
|
|
|
|
|
|
|
|
if err != nil { |
|
|
|
|
|
t.Errorf("Expected nil, recieved %s", err.Error()) |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// Fetch Request
|
|
|
|
|
|
res, err := http.DefaultClient.Do(req) |
|
|
|
|
|
if err != nil { |
|
|
|
|
|
t.Errorf("Expected nil, recieved %s", err.Error()) |
|
|
|
|
|
return |
|
|
|
|
|
} |
|
|
|
|
|
defer res.Body.Close() |
|
|
|
|
|
|
|
|
Database.DB.Model(Models.User{}). |
|
|
|
|
|
Select("count(*) > 0"). |
|
|
|
|
|
Where("email = ?", "email@email.com"). |
|
|
|
|
|
Delete(Models.User{}) |
|
|
|
|
|
|
|
|
if res.StatusCode != http.StatusOK { |
|
|
|
|
|
t.Errorf("Expected %d, recieved %d", http.StatusOK, res.StatusCode) |
|
|
|
|
|
} |
|
|
} |
|
|
} |