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.

128 lines
3.4 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/Envelope/Backend/Api/Auth"
  8. "git.tovijaeschke.xyz/tovi/Envelope/Backend/Database"
  9. "git.tovijaeschke.xyz/tovi/Envelope/Backend/Tests"
  10. )
  11. func Test_ChangePassword(t *testing.T) {
  12. client, 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. OldPassword string `json:"old_password"`
  20. NewPassword string `json:"new_password"`
  21. NewPasswordConfirm string `json:"new_password_confirm"`
  22. PrivateKey string `json:"private_key"`
  23. }{
  24. OldPassword: "password",
  25. NewPassword: "password1",
  26. NewPasswordConfirm: "password1",
  27. PrivateKey: "",
  28. }
  29. jsonStr, _ := json.Marshal(d)
  30. req, _ := http.NewRequest("POST", ts.URL+"/api/v1/auth/change_password", bytes.NewBuffer(jsonStr))
  31. req.Header.Set("Content-Type", "application/json")
  32. resp, err := client.Do(req)
  33. if err != nil {
  34. t.Errorf("Expected nil, recieved %s", err.Error())
  35. return
  36. }
  37. if resp.StatusCode != http.StatusNoContent {
  38. t.Errorf("Expected %d, recieved %d", http.StatusNoContent, resp.StatusCode)
  39. return
  40. }
  41. u, err := Database.GetUserByUsername("test")
  42. if err != nil {
  43. t.Errorf("Expected nil, recieved %s", err.Error())
  44. return
  45. }
  46. if !Auth.CheckPasswordHash("password1", u.Password) {
  47. t.Errorf("Failed to verify the password has been changed")
  48. }
  49. }
  50. func Test_ChangePasswordMismatchConfirmFails(t *testing.T) {
  51. client, 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. OldPassword string `json:"old_password"`
  59. NewPassword string `json:"new_password"`
  60. NewPasswordConfirm string `json:"new_password_confirm"`
  61. PrivateKey string `json:"private_key"`
  62. }{
  63. OldPassword: "password",
  64. NewPassword: "password1",
  65. NewPasswordConfirm: "password2",
  66. PrivateKey: "",
  67. }
  68. jsonStr, _ := json.Marshal(d)
  69. req, _ := http.NewRequest("POST", ts.URL+"/api/v1/auth/change_password", bytes.NewBuffer(jsonStr))
  70. req.Header.Set("Content-Type", "application/json")
  71. resp, err := client.Do(req)
  72. if err != nil {
  73. t.Errorf("Expected nil, recieved %s", err.Error())
  74. return
  75. }
  76. if resp.StatusCode != http.StatusUnprocessableEntity {
  77. t.Errorf("Expected %d, recieved %d", http.StatusUnprocessableEntity, resp.StatusCode)
  78. }
  79. }
  80. func Test_ChangePasswordInvalidCurrentPasswordFails(t *testing.T) {
  81. client, ts, err := Tests.InitTestEnv()
  82. defer ts.Close()
  83. if err != nil {
  84. t.Errorf("Expected nil, recieved %s", err.Error())
  85. return
  86. }
  87. d := struct {
  88. OldPassword string `json:"old_password"`
  89. NewPassword string `json:"new_password"`
  90. NewPasswordConfirm string `json:"new_password_confirm"`
  91. PrivateKey string `json:"private_key"`
  92. }{
  93. OldPassword: "password2",
  94. NewPassword: "password1",
  95. NewPasswordConfirm: "password1",
  96. PrivateKey: "",
  97. }
  98. jsonStr, _ := json.Marshal(d)
  99. req, _ := http.NewRequest("POST", ts.URL+"/api/v1/auth/change_password", bytes.NewBuffer(jsonStr))
  100. req.Header.Set("Content-Type", "application/json")
  101. resp, err := client.Do(req)
  102. if err != nil {
  103. t.Errorf("Expected nil, recieved %s", err.Error())
  104. return
  105. }
  106. if resp.StatusCode != http.StatusForbidden {
  107. t.Errorf("Expected %d, recieved %d", http.StatusForbidden, resp.StatusCode)
  108. }
  109. }