Encrypted messaging app
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.

95 lines
2.0 KiB

2 years ago
2 years ago
2 years ago
2 years ago
  1. package Auth_test
  2. import (
  3. "bytes"
  4. "encoding/json"
  5. "net/http"
  6. "testing"
  7. "git.tovijaeschke.xyz/tovi/Capsule/Backend/Database"
  8. "git.tovijaeschke.xyz/tovi/Capsule/Backend/Models"
  9. "git.tovijaeschke.xyz/tovi/Capsule/Backend/Tests"
  10. )
  11. func Test_Login(t *testing.T) {
  12. _, ts, err := Tests.InitTestEnv()
  13. defer ts.Close()
  14. if err != nil {
  15. t.Errorf("Expected nil, recieved %s", err.Error())
  16. return
  17. }
  18. d := struct {
  19. Username string `json:"username"`
  20. Password string `json:"password"`
  21. }{
  22. Username: "test",
  23. Password: "password",
  24. }
  25. jsonStr, _ := json.Marshal(d)
  26. req, _ := http.NewRequest("POST", ts.URL+"/api/v1/login", bytes.NewBuffer(jsonStr))
  27. req.Header.Set("Content-Type", "application/json")
  28. client := &http.Client{}
  29. resp, err := client.Do(req)
  30. if err != nil {
  31. t.Errorf("Expected nil, recieved %s", err.Error())
  32. return
  33. }
  34. if resp.StatusCode != http.StatusOK {
  35. t.Errorf("Expected %d, recieved %d", http.StatusOK, resp.StatusCode)
  36. return
  37. }
  38. u, err := Database.GetUserByUsername("test")
  39. if err != nil {
  40. t.Errorf("Expected nil, recieved %s", err.Error())
  41. return
  42. }
  43. var session Models.Session
  44. err = Database.DB.First(&session, "user_id = ?", u.ID.String()).Error
  45. if err != nil {
  46. t.Errorf("Expected user record, recieved %s", err.Error())
  47. return
  48. }
  49. }
  50. func Test_Login_PasswordFails(t *testing.T) {
  51. _, ts, err := Tests.InitTestEnv()
  52. defer ts.Close()
  53. if err != nil {
  54. t.Errorf("Expected nil, recieved %s", err.Error())
  55. return
  56. }
  57. d := struct {
  58. Username string `json:"username"`
  59. Password string `json:"password"`
  60. }{
  61. Username: "test",
  62. Password: "password1",
  63. }
  64. jsonStr, _ := json.Marshal(d)
  65. req, _ := http.NewRequest("POST", ts.URL+"/api/v1/login", bytes.NewBuffer(jsonStr))
  66. req.Header.Set("Content-Type", "application/json")
  67. client := &http.Client{}
  68. resp, err := client.Do(req)
  69. if err != nil {
  70. t.Errorf("Expected nil, recieved %s", err.Error())
  71. return
  72. }
  73. if resp.StatusCode != http.StatusUnauthorized {
  74. t.Errorf("Expected %d, recieved %d", http.StatusUnauthorized, resp.StatusCode)
  75. return
  76. }
  77. }