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.

94 lines
2.0 KiB

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