You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

132 lines
2.6 KiB

  1. package Api
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "io/ioutil"
  6. "log"
  7. "math/rand"
  8. "net/http"
  9. "net/http/httptest"
  10. "os"
  11. "path"
  12. "runtime"
  13. "strings"
  14. "testing"
  15. "git.tovijaeschke.xyz/tovi/SuddenImpactRecords/Database"
  16. "git.tovijaeschke.xyz/tovi/SuddenImpactRecords/Models"
  17. "github.com/gorilla/mux"
  18. "gorm.io/gorm"
  19. )
  20. func init() {
  21. // Fix working directory for tests
  22. _, filename, _, _ := runtime.Caller(0)
  23. dir := path.Join(path.Dir(filename), "..")
  24. err := os.Chdir(dir)
  25. if err != nil {
  26. panic(err)
  27. }
  28. log.SetOutput(ioutil.Discard)
  29. Database.Init()
  30. r = mux.NewRouter()
  31. }
  32. var letterRunes = []rune("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ")
  33. func RandStringRunes(n int) string {
  34. b := make([]rune, n)
  35. for i := range b {
  36. b[i] = letterRunes[rand.Intn(len(letterRunes))]
  37. }
  38. return string(b)
  39. }
  40. func Test_getUsers(t *testing.T) {
  41. t.Log("Testing getUsers...")
  42. r.HandleFunc("/user", getUsers).Methods("GET")
  43. ts := httptest.NewServer(r)
  44. defer ts.Close()
  45. var err error
  46. for i := 0; i < 20; i++ {
  47. userData := Models.User{
  48. Email: fmt.Sprintf(
  49. "%s@email.com",
  50. RandStringRunes(16),
  51. ),
  52. Password: "password",
  53. ConfirmPassword: "password",
  54. }
  55. err = Database.CreateUser(&userData)
  56. if err != nil {
  57. t.Errorf("Expected nil, recieved %s", err.Error())
  58. }
  59. defer Database.DB.
  60. Session(&gorm.Session{FullSaveAssociations: true}).
  61. Unscoped().
  62. Delete(&userData)
  63. }
  64. res, err := http.Get(ts.URL + "/user?page=1&pageSize=10")
  65. if err != nil {
  66. t.Errorf("Expected nil, recieved %s", err.Error())
  67. }
  68. if res.StatusCode != http.StatusOK {
  69. t.Errorf("Expected %d, recieved %d", http.StatusOK, res.StatusCode)
  70. }
  71. getUsersData := new([]Models.User)
  72. err = json.NewDecoder(res.Body).Decode(getUsersData)
  73. if err != nil {
  74. t.Errorf("Expected nil, recieved %s", err.Error())
  75. }
  76. if len(*getUsersData) != 10 {
  77. t.Errorf("Expected 10, recieved %d", len(*getUsersData))
  78. }
  79. }
  80. func Test_createUser(t *testing.T) {
  81. t.Log("Testing createUser...")
  82. r.HandleFunc("/user", createUser).Methods("POST")
  83. ts := httptest.NewServer(r)
  84. defer ts.Close()
  85. postJson := `
  86. {
  87. "email": "email@email.com",
  88. "password": "password",
  89. "confirm_password": "password",
  90. "first_name": "Hugh",
  91. "last_name": "Mann"
  92. }
  93. `
  94. res, err := http.Post(ts.URL+"/user", "application/json", strings.NewReader(postJson))
  95. if err != nil {
  96. t.Errorf("Expected nil, recieved %s", err.Error())
  97. return
  98. }
  99. if res.StatusCode != http.StatusOK {
  100. t.Errorf("Expected %d, recieved %d", http.StatusOK, res.StatusCode)
  101. return
  102. }
  103. Database.DB.Model(Models.User{}).
  104. Select("count(*) > 0").
  105. Where("email = ?", "email@email.com").
  106. Delete(Models.User{})
  107. }