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.

255 lines
6.4 KiB

2 years ago
  1. package Messages_test
  2. import (
  3. "encoding/base64"
  4. "encoding/json"
  5. "fmt"
  6. "io/ioutil"
  7. "net/http"
  8. "testing"
  9. "git.tovijaeschke.xyz/tovi/Capsule/Backend/Database"
  10. "git.tovijaeschke.xyz/tovi/Capsule/Backend/Database/Seeder"
  11. "git.tovijaeschke.xyz/tovi/Capsule/Backend/Models"
  12. "git.tovijaeschke.xyz/tovi/Capsule/Backend/Tests"
  13. )
  14. func Test_ConversationsList(t *testing.T) {
  15. client, ts, err := Tests.InitTestEnv()
  16. if err != nil {
  17. t.Errorf("Expected nil, recieved %s", err.Error())
  18. return
  19. }
  20. u, err := Database.GetUserByUsername("test")
  21. key, err := Seeder.GenerateAesKey()
  22. if err != nil {
  23. t.Errorf("Expected nil, recieved %s", err.Error())
  24. return
  25. }
  26. nameCiphertext, err := key.AesEncrypt([]byte("Test conversation"))
  27. if err != nil {
  28. t.Errorf("Expected nil, recieved %s", err.Error())
  29. return
  30. }
  31. twoUserCiphertext, err := key.AesEncrypt([]byte("false"))
  32. if err != nil {
  33. t.Errorf("Expected nil, recieved %s", err.Error())
  34. return
  35. }
  36. messageThread := Models.ConversationDetail{
  37. Name: base64.StdEncoding.EncodeToString(nameCiphertext),
  38. TwoUser: base64.StdEncoding.EncodeToString(twoUserCiphertext),
  39. }
  40. err = Database.CreateConversationDetail(&messageThread)
  41. if err != nil {
  42. t.Errorf("Expected nil, recieved %s", err.Error())
  43. return
  44. }
  45. conversationDetailIDCiphertext, err := key.AesEncrypt([]byte(messageThread.ID.String()))
  46. if err != nil {
  47. t.Errorf("Expected nil, recieved %s", err.Error())
  48. return
  49. }
  50. adminCiphertext, err := key.AesEncrypt([]byte("true"))
  51. if err != nil {
  52. t.Errorf("Expected nil, recieved %s", err.Error())
  53. return
  54. }
  55. pubKey := Seeder.GetPubKey()
  56. messageThreadUser := Models.UserConversation{
  57. UserID: u.ID,
  58. ConversationDetailID: base64.StdEncoding.EncodeToString(conversationDetailIDCiphertext),
  59. Admin: base64.StdEncoding.EncodeToString(adminCiphertext),
  60. SymmetricKey: base64.StdEncoding.EncodeToString(
  61. Seeder.EncryptWithPublicKey(key.Key, pubKey),
  62. ),
  63. }
  64. err = Database.CreateUserConversation(&messageThreadUser)
  65. req, _ := http.NewRequest("GET", ts.URL+"/api/v1/auth/conversations", nil)
  66. resp, err := client.Do(req)
  67. if err != nil {
  68. t.Errorf("Expected nil, recieved %s", err.Error())
  69. return
  70. }
  71. if resp.StatusCode != http.StatusOK {
  72. t.Errorf("Expected %d, recieved %d", http.StatusOK, resp.StatusCode)
  73. return
  74. }
  75. requestBody, err := ioutil.ReadAll(resp.Body)
  76. if err != nil {
  77. t.Errorf("Expected %d, recieved %d", http.StatusOK, resp.StatusCode)
  78. return
  79. }
  80. var conversations []Models.UserConversation
  81. json.Unmarshal(requestBody, &conversations)
  82. if len(conversations) != 1 {
  83. t.Errorf("Expected %d, recieved %d", 1, len(conversations))
  84. }
  85. conv := conversations[0]
  86. decodedId, err := base64.StdEncoding.DecodeString(conv.ConversationDetailID)
  87. if err != nil {
  88. t.Errorf("Expected %d, recieved %d", http.StatusOK, resp.StatusCode)
  89. return
  90. }
  91. decrypedId, err := key.AesDecrypt(decodedId)
  92. if err != nil {
  93. t.Errorf("Expected %d, recieved %d", http.StatusOK, resp.StatusCode)
  94. return
  95. }
  96. req, _ = http.NewRequest(
  97. "GET",
  98. ts.URL+"/api/v1/auth/conversation_details?conversation_detail_ids="+string(decrypedId),
  99. nil,
  100. )
  101. resp, err = client.Do(req)
  102. if err != nil {
  103. t.Errorf("Expected nil, recieved %s", err.Error())
  104. return
  105. }
  106. var conversationDetails []Models.ConversationDetail
  107. requestBody, err = ioutil.ReadAll(resp.Body)
  108. if err != nil {
  109. t.Errorf("Expected %d, recieved %d", http.StatusOK, resp.StatusCode)
  110. return
  111. }
  112. json.Unmarshal(requestBody, &conversationDetails)
  113. if len(conversationDetails) != 1 {
  114. t.Errorf("Expected %d, recieved %d", 1, len(conversations))
  115. }
  116. decodedName, err := base64.StdEncoding.DecodeString(conversationDetails[0].Name)
  117. if err != nil {
  118. t.Errorf("Expected %d, recieved %d", http.StatusOK, resp.StatusCode)
  119. return
  120. }
  121. decrypedName, err := key.AesDecrypt(decodedName)
  122. if err != nil {
  123. t.Errorf("Expected %d, recieved %d", http.StatusOK, resp.StatusCode)
  124. return
  125. }
  126. if string(decrypedName) != "Test conversation" {
  127. t.Errorf("Expected %s, recieved %s", "Test converation", string(decrypedName))
  128. }
  129. }
  130. func Test_ConversationsListPagination(t *testing.T) {
  131. client, ts, err := Tests.InitTestEnv()
  132. if err != nil {
  133. t.Errorf("Expected nil, recieved %s", err.Error())
  134. return
  135. }
  136. u, err := Database.GetUserByUsername("test")
  137. key, err := Seeder.GenerateAesKey()
  138. if err != nil {
  139. t.Errorf("Expected nil, recieved %s", err.Error())
  140. return
  141. }
  142. for i := 0; i < 40; i++ {
  143. nameCiphertext, err := key.AesEncrypt([]byte(
  144. fmt.Sprintf("Test conversation %d", i),
  145. ))
  146. if err != nil {
  147. t.Errorf("Expected nil, recieved %s", err.Error())
  148. return
  149. }
  150. twoUserCiphertext, err := key.AesEncrypt([]byte("false"))
  151. if err != nil {
  152. t.Errorf("Expected nil, recieved %s", err.Error())
  153. return
  154. }
  155. messageThread := Models.ConversationDetail{
  156. Name: base64.StdEncoding.EncodeToString(nameCiphertext),
  157. TwoUser: base64.StdEncoding.EncodeToString(twoUserCiphertext),
  158. }
  159. err = Database.CreateConversationDetail(&messageThread)
  160. if err != nil {
  161. t.Errorf("Expected nil, recieved %s", err.Error())
  162. return
  163. }
  164. conversationDetailIDCiphertext, err := key.AesEncrypt([]byte(messageThread.ID.String()))
  165. if err != nil {
  166. t.Errorf("Expected nil, recieved %s", err.Error())
  167. return
  168. }
  169. adminCiphertext, err := key.AesEncrypt([]byte("true"))
  170. if err != nil {
  171. t.Errorf("Expected nil, recieved %s", err.Error())
  172. return
  173. }
  174. pubKey := Seeder.GetPubKey()
  175. messageThreadUser := Models.UserConversation{
  176. UserID: u.ID,
  177. ConversationDetailID: base64.StdEncoding.EncodeToString(conversationDetailIDCiphertext),
  178. Admin: base64.StdEncoding.EncodeToString(adminCiphertext),
  179. SymmetricKey: base64.StdEncoding.EncodeToString(
  180. Seeder.EncryptWithPublicKey(key.Key, pubKey),
  181. ),
  182. }
  183. err = Database.CreateUserConversation(&messageThreadUser)
  184. }
  185. req, _ := http.NewRequest("GET", ts.URL+"/api/v1/auth/conversations?page=0", nil)
  186. resp, err := client.Do(req)
  187. if err != nil {
  188. t.Errorf("Expected nil, recieved %s", err.Error())
  189. return
  190. }
  191. if resp.StatusCode != http.StatusOK {
  192. t.Errorf("Expected %d, recieved %d", http.StatusOK, resp.StatusCode)
  193. return
  194. }
  195. requestBody, err := ioutil.ReadAll(resp.Body)
  196. if err != nil {
  197. t.Errorf("Expected %d, recieved %d", http.StatusOK, resp.StatusCode)
  198. return
  199. }
  200. var conversations []Models.UserConversation
  201. json.Unmarshal(requestBody, &conversations)
  202. if len(conversations) != 20 {
  203. t.Errorf("Expected %d, recieved %d", 1, len(conversations))
  204. }
  205. }