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.

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