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.

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