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.

140 lines
3.1 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/Api/Auth"
  13. "git.tovijaeschke.xyz/tovi/Capsule/Backend/Database"
  14. "git.tovijaeschke.xyz/tovi/Capsule/Backend/Database/Seeder"
  15. "git.tovijaeschke.xyz/tovi/Capsule/Backend/Models"
  16. "github.com/gorilla/mux"
  17. )
  18. func Test_Login(t *testing.T) {
  19. log.SetOutput(ioutil.Discard)
  20. Database.InitTest()
  21. r := mux.NewRouter()
  22. Api.InitAPIEndpoints(r)
  23. ts := httptest.NewServer(r)
  24. defer ts.Close()
  25. userKey, _ := Seeder.GenerateAesKey()
  26. pubKey := Seeder.GetPubKey()
  27. p, _ := Auth.HashPassword("password")
  28. u := Models.User{
  29. Username: "test",
  30. Password: p,
  31. AsymmetricPublicKey: Seeder.PublicKey,
  32. AsymmetricPrivateKey: Seeder.EncryptedPrivateKey,
  33. SymmetricKey: base64.StdEncoding.EncodeToString(
  34. Seeder.EncryptWithPublicKey(userKey.Key, pubKey),
  35. ),
  36. }
  37. err := Database.CreateUser(&u)
  38. if err != nil {
  39. t.Errorf("Expected nil, recieved %s", err.Error())
  40. return
  41. }
  42. d := struct {
  43. Username string `json:"username"`
  44. Password string `json:"password"`
  45. }{
  46. Username: "test",
  47. Password: "password",
  48. }
  49. jsonStr, _ := json.Marshal(d)
  50. req, _ := http.NewRequest("POST", ts.URL+"/api/v1/login", bytes.NewBuffer(jsonStr))
  51. req.Header.Set("Content-Type", "application/json")
  52. client := &http.Client{}
  53. resp, err := client.Do(req)
  54. if err != nil {
  55. t.Errorf("Expected nil, recieved %s", err.Error())
  56. return
  57. }
  58. if resp.StatusCode != http.StatusOK {
  59. t.Errorf("Expected %d, recieved %d", http.StatusOK, resp.StatusCode)
  60. return
  61. }
  62. var session Models.Session
  63. err = Database.DB.First(&session, "user_id = ?", u.ID.String()).Error
  64. if err != nil {
  65. t.Errorf("Expected user record, recieved %s", err.Error())
  66. return
  67. }
  68. }
  69. func Test_Login_PasswordFails(t *testing.T) {
  70. log.SetOutput(ioutil.Discard)
  71. Database.InitTest()
  72. r := mux.NewRouter()
  73. Api.InitAPIEndpoints(r)
  74. ts := httptest.NewServer(r)
  75. defer ts.Close()
  76. userKey, _ := Seeder.GenerateAesKey()
  77. pubKey := Seeder.GetPubKey()
  78. p, _ := Auth.HashPassword("password")
  79. u := Models.User{
  80. Username: "test",
  81. Password: p,
  82. AsymmetricPublicKey: Seeder.PublicKey,
  83. AsymmetricPrivateKey: Seeder.EncryptedPrivateKey,
  84. SymmetricKey: base64.StdEncoding.EncodeToString(
  85. Seeder.EncryptWithPublicKey(userKey.Key, pubKey),
  86. ),
  87. }
  88. err := Database.CreateUser(&u)
  89. if err != nil {
  90. t.Errorf("Expected nil, recieved %s", err.Error())
  91. return
  92. }
  93. d := struct {
  94. Username string `json:"username"`
  95. Password string `json:"password"`
  96. }{
  97. Username: "test",
  98. Password: "password1",
  99. }
  100. jsonStr, _ := json.Marshal(d)
  101. req, _ := http.NewRequest("POST", ts.URL+"/api/v1/login", bytes.NewBuffer(jsonStr))
  102. req.Header.Set("Content-Type", "application/json")
  103. client := &http.Client{}
  104. resp, err := client.Do(req)
  105. if err != nil {
  106. t.Errorf("Expected nil, recieved %s", err.Error())
  107. return
  108. }
  109. if resp.StatusCode != http.StatusUnauthorized {
  110. t.Errorf("Expected %d, recieved %d", http.StatusUnauthorized, resp.StatusCode)
  111. return
  112. }
  113. }