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.

182 lines
4.7 KiB

package Messages_test
import (
"bytes"
"encoding/base64"
"encoding/json"
"net/http"
"testing"
"git.tovijaeschke.xyz/tovi/Envelope/Backend/Database"
"git.tovijaeschke.xyz/tovi/Envelope/Backend/Database/Seeder"
"git.tovijaeschke.xyz/tovi/Envelope/Backend/Tests"
)
func createConversation(key Seeder.AesKey) (Database.ConversationDetail, Database.UserConversation, Database.ConversationDetailUser, error) {
var (
cd Database.ConversationDetail
uc Database.UserConversation
cdu Database.ConversationDetailUser
)
u, err := Database.GetUserByUsername("test")
nameCiphertext, err := key.AesEncrypt([]byte("Test conversation"))
if err != nil {
return cd, uc, cdu, err
}
twoUserCiphertext, err := key.AesEncrypt([]byte("false"))
if err != nil {
return cd, uc, cdu, err
}
cd = Database.ConversationDetail{
Name: base64.StdEncoding.EncodeToString(nameCiphertext),
TwoUser: base64.StdEncoding.EncodeToString(twoUserCiphertext),
}
err = (&cd).CreateConversationDetail()
if err != nil {
return cd, uc, cdu, err
}
conversationDetailIDCiphertext, err := key.AesEncrypt([]byte(cd.ID.String()))
if err != nil {
return cd, uc, cdu, err
}
adminCiphertext, err := key.AesEncrypt([]byte("true"))
if err != nil {
return cd, uc, cdu, err
}
pubKey := Seeder.GetPubKey()
uc = Database.UserConversation{
UserID: u.ID,
ConversationDetailID: base64.StdEncoding.EncodeToString(conversationDetailIDCiphertext),
Admin: base64.StdEncoding.EncodeToString(adminCiphertext),
SymmetricKey: base64.StdEncoding.EncodeToString(
Seeder.EncryptWithPublicKey(key.Key, pubKey),
),
}
err = (&uc).CreateUserConversation()
if err != nil {
return cd, uc, cdu, err
}
userIDCiphertext, err := key.AesEncrypt([]byte(u.ID.String()))
if err != nil {
return cd, uc, cdu, err
}
usernameCiphertext, err := key.AesEncrypt([]byte(u.Username))
if err != nil {
return cd, uc, cdu, err
}
adminCiphertext, err = key.AesEncrypt([]byte("true"))
if err != nil {
return cd, uc, cdu, err
}
associationKeyCiphertext, err := key.AesEncrypt([]byte("association"))
if err != nil {
return cd, uc, cdu, err
}
publicKeyCiphertext, err := key.AesEncrypt([]byte(u.AsymmetricPublicKey))
if err != nil {
return cd, uc, cdu, err
}
cdu = Database.ConversationDetailUser{
ConversationDetailID: cd.ID,
UserID: base64.StdEncoding.EncodeToString(userIDCiphertext),
Username: base64.StdEncoding.EncodeToString(usernameCiphertext),
Admin: base64.StdEncoding.EncodeToString(adminCiphertext),
AssociationKey: base64.StdEncoding.EncodeToString(associationKeyCiphertext),
PublicKey: base64.StdEncoding.EncodeToString(publicKeyCiphertext),
}
err = (&cdu).CreateConversationDetailUser()
return cd, uc, cdu, err
}
func Test_UpdateConversation(t *testing.T) {
client, ts, err := Tests.InitTestEnv()
if err != nil {
t.Errorf("Expected nil, recieved %s", err.Error())
return
}
// u, err := Database.GetUserByUsername("test")
key, err := Seeder.GenerateAesKey()
if err != nil {
t.Errorf("Expected nil, recieved %s", err.Error())
return
}
cd, uc, cdu, err := createConversation(key)
nameCiphertext, err := key.AesEncrypt([]byte("Not test conversation"))
if err != nil {
t.Errorf("Expected nil, recieved %s", err.Error())
}
d := struct {
ID string `json:"id"`
Name string `json:"name"`
Users []Database.ConversationDetailUser
UserConversations []Database.UserConversation
}{
ID: cd.ID.String(),
Name: base64.StdEncoding.EncodeToString(nameCiphertext),
Users: []Database.ConversationDetailUser{
cdu,
},
UserConversations: []Database.UserConversation{
uc,
},
}
jsonStr, _ := json.Marshal(d)
req, _ := http.NewRequest("PUT", ts.URL+"/api/v1/auth/conversations", bytes.NewBuffer(jsonStr))
req.Header.Set("Content-Type", "application/json")
resp, err := client.Do(req)
if err != nil {
t.Errorf("Expected nil, recieved %s", err.Error())
return
}
if resp.StatusCode != http.StatusNoContent {
t.Errorf("Expected %d, recieved %d", http.StatusNoContent, resp.StatusCode)
}
var ncd Database.ConversationDetail
err = Database.DB.First(&ncd, "id = ?", cd.ID.String()).Error
if err != nil {
t.Errorf("Expected nil, recieved %s", err.Error())
return
}
decodedName, err := base64.StdEncoding.DecodeString(ncd.Name)
if err != nil {
t.Errorf("Expected nil, recieved %s", err.Error())
return
}
decrypedName, err := key.AesDecrypt(decodedName)
if err != nil {
t.Errorf("Expected %d, recieved %d", http.StatusOK, resp.StatusCode)
return
}
if string(decrypedName) != "Not test conversation" {
t.Errorf("Expected %s, recieved %s", "Not test converation", string(decrypedName))
}
}