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.

306 lines
6.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. "testing"
  13. "time"
  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. func Test_ChangePassword(t *testing.T) {
  22. log.SetOutput(ioutil.Discard)
  23. Database.InitTest()
  24. r := mux.NewRouter()
  25. Api.InitAPIEndpoints(r)
  26. ts := httptest.NewServer(r)
  27. defer ts.Close()
  28. userKey, _ := Seeder.GenerateAesKey()
  29. pubKey := Seeder.GetPubKey()
  30. p, _ := Auth.HashPassword("password")
  31. u := Models.User{
  32. Username: "test",
  33. Password: p,
  34. AsymmetricPublicKey: Seeder.PublicKey,
  35. AsymmetricPrivateKey: Seeder.EncryptedPrivateKey,
  36. SymmetricKey: base64.StdEncoding.EncodeToString(
  37. Seeder.EncryptWithPublicKey(userKey.Key, pubKey),
  38. ),
  39. }
  40. err := Database.CreateUser(&u)
  41. if err != nil {
  42. t.Errorf("Expected nil, recieved %s", err.Error())
  43. return
  44. }
  45. session := Models.Session{
  46. UserID: u.ID,
  47. Expiry: time.Now().Add(12 * time.Hour),
  48. }
  49. err = Database.CreateSession(&session)
  50. if err != nil {
  51. t.Errorf("Expected nil, recieved %s", err.Error())
  52. return
  53. }
  54. jar, err := cookiejar.New(nil)
  55. if err != nil {
  56. t.Errorf("Expected nil, recieved %s", err.Error())
  57. return
  58. }
  59. url, _ := url.Parse(ts.URL)
  60. jar.SetCookies(
  61. url,
  62. []*http.Cookie{
  63. {
  64. Name: "session_token",
  65. Value: session.ID.String(),
  66. MaxAge: 300,
  67. },
  68. },
  69. )
  70. d := struct {
  71. OldPassword string `json:"old_password"`
  72. NewPassword string `json:"new_password"`
  73. NewPasswordConfirm string `json:"new_password_confirm"`
  74. PrivateKey string `json:"private_key"`
  75. }{
  76. OldPassword: "password",
  77. NewPassword: "password1",
  78. NewPasswordConfirm: "password1",
  79. PrivateKey: "",
  80. }
  81. jsonStr, _ := json.Marshal(d)
  82. req, _ := http.NewRequest("POST", ts.URL+"/api/v1/auth/change_password", bytes.NewBuffer(jsonStr))
  83. req.Header.Set("Content-Type", "application/json")
  84. client := &http.Client{
  85. Jar: jar,
  86. }
  87. resp, err := client.Do(req)
  88. if err != nil {
  89. t.Errorf("Expected nil, recieved %s", err.Error())
  90. return
  91. }
  92. if resp.StatusCode != http.StatusNoContent {
  93. t.Errorf("Expected %d, recieved %d", http.StatusNoContent, resp.StatusCode)
  94. return
  95. }
  96. u, err = Database.GetUserById(u.ID.String())
  97. if err != nil {
  98. t.Errorf("Expected nil, recieved %s", err.Error())
  99. return
  100. }
  101. if !Auth.CheckPasswordHash("password1", u.Password) {
  102. t.Errorf("Failed to verify the password has been changed")
  103. }
  104. }
  105. func Test_ChangePasswordMismatchConfirmFails(t *testing.T) {
  106. log.SetOutput(ioutil.Discard)
  107. Database.InitTest()
  108. r := mux.NewRouter()
  109. Api.InitAPIEndpoints(r)
  110. ts := httptest.NewServer(r)
  111. defer ts.Close()
  112. userKey, _ := Seeder.GenerateAesKey()
  113. pubKey := Seeder.GetPubKey()
  114. p, _ := Auth.HashPassword("password")
  115. u := Models.User{
  116. Username: "test",
  117. Password: p,
  118. AsymmetricPublicKey: Seeder.PublicKey,
  119. AsymmetricPrivateKey: Seeder.EncryptedPrivateKey,
  120. SymmetricKey: base64.StdEncoding.EncodeToString(
  121. Seeder.EncryptWithPublicKey(userKey.Key, pubKey),
  122. ),
  123. }
  124. err := Database.CreateUser(&u)
  125. if err != nil {
  126. t.Errorf("Expected nil, recieved %s", err.Error())
  127. return
  128. }
  129. session := Models.Session{
  130. UserID: u.ID,
  131. Expiry: time.Now().Add(12 * time.Hour),
  132. }
  133. err = Database.CreateSession(&session)
  134. if err != nil {
  135. t.Errorf("Expected nil, recieved %s", err.Error())
  136. return
  137. }
  138. jar, err := cookiejar.New(nil)
  139. if err != nil {
  140. t.Errorf("Expected nil, recieved %s", err.Error())
  141. return
  142. }
  143. url, _ := url.Parse(ts.URL)
  144. jar.SetCookies(
  145. url,
  146. []*http.Cookie{
  147. {
  148. Name: "session_token",
  149. Value: session.ID.String(),
  150. MaxAge: 300,
  151. },
  152. },
  153. )
  154. d := struct {
  155. OldPassword string `json:"old_password"`
  156. NewPassword string `json:"new_password"`
  157. NewPasswordConfirm string `json:"new_password_confirm"`
  158. PrivateKey string `json:"private_key"`
  159. }{
  160. OldPassword: "password",
  161. NewPassword: "password1",
  162. NewPasswordConfirm: "password2",
  163. PrivateKey: "",
  164. }
  165. jsonStr, _ := json.Marshal(d)
  166. req, _ := http.NewRequest("POST", ts.URL+"/api/v1/auth/change_password", bytes.NewBuffer(jsonStr))
  167. req.Header.Set("Content-Type", "application/json")
  168. client := &http.Client{
  169. Jar: jar,
  170. }
  171. resp, err := client.Do(req)
  172. if err != nil {
  173. t.Errorf("Expected nil, recieved %s", err.Error())
  174. return
  175. }
  176. if resp.StatusCode != http.StatusUnprocessableEntity {
  177. t.Errorf("Expected %d, recieved %d", http.StatusUnprocessableEntity, resp.StatusCode)
  178. }
  179. }
  180. func Test_ChangePasswordInvalidCurrentPasswordFails(t *testing.T) {
  181. log.SetOutput(ioutil.Discard)
  182. Database.InitTest()
  183. r := mux.NewRouter()
  184. Api.InitAPIEndpoints(r)
  185. ts := httptest.NewServer(r)
  186. defer ts.Close()
  187. userKey, _ := Seeder.GenerateAesKey()
  188. pubKey := Seeder.GetPubKey()
  189. p, _ := Auth.HashPassword("password")
  190. u := Models.User{
  191. Username: "test",
  192. Password: p,
  193. AsymmetricPublicKey: Seeder.PublicKey,
  194. AsymmetricPrivateKey: Seeder.EncryptedPrivateKey,
  195. SymmetricKey: base64.StdEncoding.EncodeToString(
  196. Seeder.EncryptWithPublicKey(userKey.Key, pubKey),
  197. ),
  198. }
  199. err := Database.CreateUser(&u)
  200. if err != nil {
  201. t.Errorf("Expected nil, recieved %s", err.Error())
  202. return
  203. }
  204. session := Models.Session{
  205. UserID: u.ID,
  206. Expiry: time.Now().Add(12 * time.Hour),
  207. }
  208. err = Database.CreateSession(&session)
  209. if err != nil {
  210. t.Errorf("Expected nil, recieved %s", err.Error())
  211. return
  212. }
  213. jar, err := cookiejar.New(nil)
  214. if err != nil {
  215. t.Errorf("Expected nil, recieved %s", err.Error())
  216. return
  217. }
  218. url, _ := url.Parse(ts.URL)
  219. jar.SetCookies(
  220. url,
  221. []*http.Cookie{
  222. {
  223. Name: "session_token",
  224. Value: session.ID.String(),
  225. MaxAge: 300,
  226. },
  227. },
  228. )
  229. d := struct {
  230. OldPassword string `json:"old_password"`
  231. NewPassword string `json:"new_password"`
  232. NewPasswordConfirm string `json:"new_password_confirm"`
  233. PrivateKey string `json:"private_key"`
  234. }{
  235. OldPassword: "password2",
  236. NewPassword: "password1",
  237. NewPasswordConfirm: "password1",
  238. PrivateKey: "",
  239. }
  240. jsonStr, _ := json.Marshal(d)
  241. req, _ := http.NewRequest("POST", ts.URL+"/api/v1/auth/change_password", bytes.NewBuffer(jsonStr))
  242. req.Header.Set("Content-Type", "application/json")
  243. client := &http.Client{
  244. Jar: jar,
  245. }
  246. resp, err := client.Do(req)
  247. if err != nil {
  248. t.Errorf("Expected nil, recieved %s", err.Error())
  249. return
  250. }
  251. if resp.StatusCode != http.StatusForbidden {
  252. t.Errorf("Expected %d, recieved %d", http.StatusForbidden, resp.StatusCode)
  253. }
  254. }