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.

117 lines
2.8 KiB

  1. package Messages_test
  2. import (
  3. "encoding/base64"
  4. "encoding/json"
  5. "io/ioutil"
  6. "net/http"
  7. "testing"
  8. "git.tovijaeschke.xyz/tovi/Capsule/Backend/Database"
  9. "git.tovijaeschke.xyz/tovi/Capsule/Backend/Database/Seeder"
  10. "git.tovijaeschke.xyz/tovi/Capsule/Backend/Models"
  11. "git.tovijaeschke.xyz/tovi/Capsule/Backend/Tests"
  12. )
  13. func Test_Messages(t *testing.T) {
  14. client, ts, err := Tests.InitTestEnv()
  15. if err != nil {
  16. t.Errorf("Expected nil, recieved %s", err.Error())
  17. return
  18. }
  19. u, err := Database.GetUserByUsername("test")
  20. userKey, err := Seeder.GenerateAesKey()
  21. if err != nil {
  22. t.Errorf("Expected nil, recieved %s", err.Error())
  23. return
  24. }
  25. key, err := Seeder.GenerateAesKey()
  26. if err != nil {
  27. t.Errorf("Expected nil, recieved %s", err.Error())
  28. return
  29. }
  30. dataCiphertext, err := key.AesEncrypt([]byte("Test message"))
  31. if err != nil {
  32. t.Errorf("Expected nil, recieved %s", err.Error())
  33. return
  34. }
  35. senderIDCiphertext, err := key.AesEncrypt([]byte(u.ID.String()))
  36. if err != nil {
  37. t.Errorf("Expected nil, recieved %s", err.Error())
  38. return
  39. }
  40. keyCiphertext, err := userKey.AesEncrypt(
  41. []byte(base64.StdEncoding.EncodeToString(key.Key)),
  42. )
  43. if err != nil {
  44. t.Errorf("Expected nil, recieved %s", err.Error())
  45. return
  46. }
  47. pubKey := Seeder.GetPubKey()
  48. message := Models.Message{
  49. MessageData: Models.MessageData{
  50. Data: base64.StdEncoding.EncodeToString(dataCiphertext),
  51. SenderID: base64.StdEncoding.EncodeToString(senderIDCiphertext),
  52. SymmetricKey: base64.StdEncoding.EncodeToString(keyCiphertext),
  53. },
  54. SymmetricKey: base64.StdEncoding.EncodeToString(
  55. Seeder.EncryptWithPublicKey(userKey.Key, pubKey),
  56. ),
  57. AssociationKey: "AssociationKey",
  58. }
  59. err = Database.CreateMessage(&message)
  60. if err != nil {
  61. t.Errorf("Expected nil, recieved %s", err.Error())
  62. return
  63. }
  64. resp, err := client.Get(ts.URL + "/api/v1/auth/messages/AssociationKey")
  65. if err != nil {
  66. t.Errorf("Expected user record, recieved %s", err.Error())
  67. return
  68. }
  69. if resp.StatusCode != http.StatusOK {
  70. t.Errorf("Expected %d, recieved %d", http.StatusOK, resp.StatusCode)
  71. return
  72. }
  73. requestBody, err := ioutil.ReadAll(resp.Body)
  74. if err != nil {
  75. t.Errorf("Expected nil, recieved %s", err.Error())
  76. return
  77. }
  78. var m []Models.Message
  79. err = json.Unmarshal(requestBody, &m)
  80. if len(m) != 1 {
  81. t.Errorf("Expected %d, recieved %d", 1, len(m))
  82. }
  83. msg := m[0]
  84. decodedData, err := base64.StdEncoding.DecodeString(msg.MessageData.Data)
  85. if err != nil {
  86. t.Errorf("Expected %d, recieved %d", http.StatusOK, resp.StatusCode)
  87. return
  88. }
  89. decrypedData, err := key.AesDecrypt(decodedData)
  90. if err != nil {
  91. t.Errorf("Expected %d, recieved %d", http.StatusOK, resp.StatusCode)
  92. return
  93. }
  94. if string(decrypedData) != "Test message" {
  95. t.Errorf("Expected %s, recieved %s", "Test converation", string(decrypedData))
  96. }
  97. }