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.

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