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.

168 lines
4.3 KiB

  1. package Auth_test
  2. import (
  3. "bytes"
  4. "encoding/base64"
  5. "encoding/json"
  6. "io/ioutil"
  7. "log"
  8. "net/http"
  9. "net/http/httptest"
  10. "testing"
  11. "git.tovijaeschke.xyz/tovi/Capsule/Backend/Api"
  12. "git.tovijaeschke.xyz/tovi/Capsule/Backend/Database"
  13. "git.tovijaeschke.xyz/tovi/Capsule/Backend/Database/Seeder"
  14. "git.tovijaeschke.xyz/tovi/Capsule/Backend/Models"
  15. "github.com/gorilla/mux"
  16. )
  17. func Test_Signup(t *testing.T) {
  18. log.SetOutput(ioutil.Discard)
  19. Database.InitTest()
  20. r := mux.NewRouter()
  21. Api.InitAPIEndpoints(r)
  22. ts := httptest.NewServer(r)
  23. defer ts.Close()
  24. userKey, _ := Seeder.GenerateAesKey()
  25. pubKey := Seeder.GetPubKey()
  26. d := struct {
  27. Username string `json:"username"`
  28. Password string `json:"password"`
  29. ConfirmPassword string `json:"confirm_password"`
  30. PubKey string `json:"asymmetric_public_key"`
  31. PrivKey string `json:"asymmetric_private_key"`
  32. SymKey string `json:"symmetric_key"`
  33. }{
  34. Username: "test",
  35. Password: "password",
  36. ConfirmPassword: "password",
  37. PubKey: Seeder.PublicKey,
  38. PrivKey: Seeder.EncryptedPrivateKey,
  39. SymKey: base64.StdEncoding.EncodeToString(
  40. Seeder.EncryptWithPublicKey(userKey.Key, pubKey),
  41. ),
  42. }
  43. jsonStr, _ := json.Marshal(d)
  44. req, _ := http.NewRequest("POST", ts.URL+"/api/v1/signup", bytes.NewBuffer(jsonStr))
  45. req.Header.Set("Content-Type", "application/json")
  46. client := &http.Client{}
  47. resp, err := client.Do(req)
  48. if err != nil {
  49. t.Errorf("Expected nil, recieved %s", err.Error())
  50. return
  51. }
  52. if resp.StatusCode != http.StatusNoContent {
  53. t.Errorf("Expected %d, recieved %d", http.StatusNoContent, resp.StatusCode)
  54. return
  55. }
  56. var user Models.User
  57. err = Database.DB.First(&user, "username = ?", "test").Error
  58. if err != nil {
  59. t.Errorf("Expected user record, recieved %s", err.Error())
  60. return
  61. }
  62. }
  63. func Test_Signup_PasswordMismatchFails(t *testing.T) {
  64. log.SetOutput(ioutil.Discard)
  65. Database.InitTest()
  66. r := mux.NewRouter()
  67. Api.InitAPIEndpoints(r)
  68. ts := httptest.NewServer(r)
  69. defer ts.Close()
  70. userKey, _ := Seeder.GenerateAesKey()
  71. pubKey := Seeder.GetPubKey()
  72. d := struct {
  73. Username string `json:"username"`
  74. Password string `json:"password"`
  75. ConfirmPassword string `json:"confirm_password"`
  76. PubKey string `json:"asymmetric_public_key"`
  77. PrivKey string `json:"asymmetric_private_key"`
  78. SymKey string `json:"symmetric_key"`
  79. }{
  80. Username: "test",
  81. Password: "password",
  82. ConfirmPassword: "password1",
  83. PubKey: Seeder.PublicKey,
  84. PrivKey: Seeder.EncryptedPrivateKey,
  85. SymKey: base64.StdEncoding.EncodeToString(
  86. Seeder.EncryptWithPublicKey(userKey.Key, pubKey),
  87. ),
  88. }
  89. jsonStr, _ := json.Marshal(d)
  90. req, _ := http.NewRequest("POST", ts.URL+"/api/v1/signup", bytes.NewBuffer(jsonStr))
  91. req.Header.Set("X-Custom-Header", "myvalue")
  92. req.Header.Set("Content-Type", "application/json")
  93. client := &http.Client{}
  94. resp, err := client.Do(req)
  95. if err != nil {
  96. t.Errorf("Expected nil, recieved %s", err.Error())
  97. return
  98. }
  99. if resp.StatusCode != http.StatusUnprocessableEntity {
  100. t.Errorf("Expected %d, recieved %d", http.StatusUnprocessableEntity, resp.StatusCode)
  101. return
  102. }
  103. }
  104. func Test_Signup_MissingDataFails(t *testing.T) {
  105. log.SetOutput(ioutil.Discard)
  106. Database.InitTest()
  107. r := mux.NewRouter()
  108. Api.InitAPIEndpoints(r)
  109. ts := httptest.NewServer(r)
  110. defer ts.Close()
  111. d := struct {
  112. Username string `json:"username"`
  113. Password string `json:"password"`
  114. ConfirmPassword string `json:"confirm_password"`
  115. PubKey string `json:"asymmetric_public_key"`
  116. PrivKey string `json:"asymmetric_private_key"`
  117. SymKey string `json:"symmetric_key"`
  118. }{
  119. Username: "test",
  120. Password: "password",
  121. ConfirmPassword: "password",
  122. PubKey: "",
  123. PrivKey: "",
  124. SymKey: "",
  125. }
  126. jsonStr, _ := json.Marshal(d)
  127. req, _ := http.NewRequest("POST", ts.URL+"/api/v1/signup", bytes.NewBuffer(jsonStr))
  128. req.Header.Set("X-Custom-Header", "myvalue")
  129. req.Header.Set("Content-Type", "application/json")
  130. client := &http.Client{}
  131. resp, err := client.Do(req)
  132. if err != nil {
  133. t.Errorf("Expected nil, recieved %s", err.Error())
  134. }
  135. if resp.StatusCode != http.StatusUnprocessableEntity {
  136. t.Errorf("Expected %d, recieved %d", http.StatusUnprocessableEntity, resp.StatusCode)
  137. }
  138. }