Browse Source

Add tests for CreateConversation and UpdateConversation

pull/4/head
Tovi Jaeschke-Rogers 2 years ago
parent
commit
43fcd3b9e8
9 changed files with 318 additions and 18 deletions
  1. +0
    -1
      Backend/Api/Auth/Logout_test.go
  2. +0
    -1
      Backend/Api/Messages/CreateConversation_test.go
  3. +2
    -0
      Backend/Api/Messages/CreateMessage_test.go
  4. +117
    -0
      Backend/Api/Messages/MessageThread_test.go
  5. +183
    -0
      Backend/Api/Messages/UpdateConversation_test.go
  6. +1
    -1
      Backend/Database/Seeder/FriendSeeder.go
  7. +5
    -5
      Backend/Database/Seeder/MessageSeeder.go
  8. +1
    -1
      Backend/Database/Seeder/UserSeeder.go
  9. +9
    -9
      Backend/Database/Seeder/encryption.go

+ 0
- 1
Backend/Api/Auth/Logout_test.go View File

@ -18,7 +18,6 @@ func Test_Logout(t *testing.T) {
} }
resp, err := client.Get(ts.URL + "/api/v1/logout") resp, err := client.Get(ts.URL + "/api/v1/logout")
if err != nil { if err != nil {
t.Errorf("Expected user record, recieved %s", err.Error()) t.Errorf("Expected user record, recieved %s", err.Error())
return return


+ 0
- 1
Backend/Api/Messages/CreateConversation_test.go View File

@ -121,7 +121,6 @@ func Test_CreateConversation(t *testing.T) {
var c Models.ConversationDetail var c Models.ConversationDetail
err = Database.DB.First(&c, "id = ?", id.String()).Error err = Database.DB.First(&c, "id = ?", id.String()).Error
if err != nil { if err != nil {
t.Errorf("Expected conversation detail record, received %s", err.Error()) t.Errorf("Expected conversation detail record, received %s", err.Error())
return return


+ 2
- 0
Backend/Api/Messages/CreateMessage_test.go View File

@ -15,6 +15,8 @@ import (
"github.com/gofrs/uuid" "github.com/gofrs/uuid"
) )
// TODO: Write test for message expiry
func Test_CreateMessage(t *testing.T) { func Test_CreateMessage(t *testing.T) {
client, ts, err := Tests.InitTestEnv() client, ts, err := Tests.InitTestEnv()
if err != nil { if err != nil {


+ 117
- 0
Backend/Api/Messages/MessageThread_test.go View File

@ -0,0 +1,117 @@
package Messages_test
import (
"encoding/base64"
"encoding/json"
"io/ioutil"
"net/http"
"testing"
"git.tovijaeschke.xyz/tovi/Capsule/Backend/Database"
"git.tovijaeschke.xyz/tovi/Capsule/Backend/Database/Seeder"
"git.tovijaeschke.xyz/tovi/Capsule/Backend/Models"
"git.tovijaeschke.xyz/tovi/Capsule/Backend/Tests"
)
func Test_Messages(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")
userKey, err := Seeder.GenerateAesKey()
if err != nil {
t.Errorf("Expected nil, recieved %s", err.Error())
return
}
key, err := Seeder.GenerateAesKey()
if err != nil {
t.Errorf("Expected nil, recieved %s", err.Error())
return
}
dataCiphertext, err := key.AesEncrypt([]byte("Test message"))
if err != nil {
t.Errorf("Expected nil, recieved %s", err.Error())
return
}
senderIDCiphertext, err := key.AesEncrypt([]byte(u.ID.String()))
if err != nil {
t.Errorf("Expected nil, recieved %s", err.Error())
return
}
keyCiphertext, err := userKey.AesEncrypt(
[]byte(base64.StdEncoding.EncodeToString(key.Key)),
)
if err != nil {
t.Errorf("Expected nil, recieved %s", err.Error())
return
}
pubKey := Seeder.GetPubKey()
message := Models.Message{
MessageData: Models.MessageData{
Data: base64.StdEncoding.EncodeToString(dataCiphertext),
SenderID: base64.StdEncoding.EncodeToString(senderIDCiphertext),
SymmetricKey: base64.StdEncoding.EncodeToString(keyCiphertext),
},
SymmetricKey: base64.StdEncoding.EncodeToString(
Seeder.EncryptWithPublicKey(userKey.Key, pubKey),
),
AssociationKey: "AssociationKey",
}
err = Database.CreateMessage(&message)
if err != nil {
t.Errorf("Expected nil, recieved %s", err.Error())
return
}
resp, err := client.Get(ts.URL + "/api/v1/auth/messages/AssociationKey")
if err != nil {
t.Errorf("Expected user record, recieved %s", err.Error())
return
}
if resp.StatusCode != http.StatusOK {
t.Errorf("Expected %d, recieved %d", http.StatusOK, resp.StatusCode)
return
}
requestBody, err := ioutil.ReadAll(resp.Body)
if err != nil {
t.Errorf("Expected nil, recieved %s", err.Error())
return
}
var m []Models.Message
err = json.Unmarshal(requestBody, &m)
if len(m) != 1 {
t.Errorf("Expected %d, recieved %d", 1, len(m))
}
msg := m[0]
decodedData, err := base64.StdEncoding.DecodeString(msg.MessageData.Data)
if err != nil {
t.Errorf("Expected %d, recieved %d", http.StatusOK, resp.StatusCode)
return
}
decrypedData, err := key.AesDecrypt(decodedData)
if err != nil {
t.Errorf("Expected %d, recieved %d", http.StatusOK, resp.StatusCode)
return
}
if string(decrypedData) != "Test message" {
t.Errorf("Expected %s, recieved %s", "Test converation", string(decrypedData))
}
}

+ 183
- 0
Backend/Api/Messages/UpdateConversation_test.go View File

@ -0,0 +1,183 @@
package Messages_test
import (
"bytes"
"encoding/base64"
"encoding/json"
"net/http"
"testing"
"git.tovijaeschke.xyz/tovi/Capsule/Backend/Database"
"git.tovijaeschke.xyz/tovi/Capsule/Backend/Database/Seeder"
"git.tovijaeschke.xyz/tovi/Capsule/Backend/Models"
"git.tovijaeschke.xyz/tovi/Capsule/Backend/Tests"
)
func createConversation(key Seeder.AesKey) (Models.ConversationDetail, Models.UserConversation, Models.ConversationDetailUser, error) {
var (
cd Models.ConversationDetail
uc Models.UserConversation
cdu Models.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 = Models.ConversationDetail{
Name: base64.StdEncoding.EncodeToString(nameCiphertext),
TwoUser: base64.StdEncoding.EncodeToString(twoUserCiphertext),
}
err = Database.CreateConversationDetail(&cd)
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 = Models.UserConversation{
UserID: u.ID,
ConversationDetailID: base64.StdEncoding.EncodeToString(conversationDetailIDCiphertext),
Admin: base64.StdEncoding.EncodeToString(adminCiphertext),
SymmetricKey: base64.StdEncoding.EncodeToString(
Seeder.EncryptWithPublicKey(key.Key, pubKey),
),
}
err = Database.CreateUserConversation(&uc)
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 = Models.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 = Database.CreateConversationDetailUser(&cdu)
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 []Models.ConversationDetailUser
UserConversations []Models.UserConversation
}{
ID: cd.ID.String(),
Name: base64.StdEncoding.EncodeToString(nameCiphertext),
Users: []Models.ConversationDetailUser{
cdu,
},
UserConversations: []Models.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 Models.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))
}
}

+ 1
- 1
Backend/Database/Seeder/FriendSeeder.go View File

@ -13,7 +13,7 @@ import (
func seedFriend(userRequestTo, userRequestFrom Models.User, accepted bool) error { func seedFriend(userRequestTo, userRequestFrom Models.User, accepted bool) error {
var ( var (
friendRequest Models.FriendRequest friendRequest Models.FriendRequest
symKey aesKey
symKey AesKey
encPublicKey []byte encPublicKey []byte
err error err error
) )


+ 5
- 5
Backend/Database/Seeder/MessageSeeder.go View File

@ -17,7 +17,7 @@ func seedMessage(
var ( var (
message Models.Message message Models.Message
messageData Models.MessageData messageData Models.MessageData
key, userKey aesKey
key, userKey AesKey
keyCiphertext []byte keyCiphertext []byte
plaintext string plaintext string
dataCiphertext []byte dataCiphertext []byte
@ -91,7 +91,7 @@ func seedMessage(
return Database.CreateMessage(&message) return Database.CreateMessage(&message)
} }
func seedConversationDetail(key aesKey) (Models.ConversationDetail, error) {
func seedConversationDetail(key AesKey) (Models.ConversationDetail, error) {
var ( var (
messageThread Models.ConversationDetail messageThread Models.ConversationDetail
name string name string
@ -124,7 +124,7 @@ func seedConversationDetail(key aesKey) (Models.ConversationDetail, error) {
func seedUserConversation( func seedUserConversation(
user Models.User, user Models.User,
threadID uuid.UUID, threadID uuid.UUID,
key aesKey,
key AesKey,
) (Models.UserConversation, error) { ) (Models.UserConversation, error) {
var ( var (
messageThreadUser Models.UserConversation messageThreadUser Models.UserConversation
@ -161,7 +161,7 @@ func seedConversationDetailUser(
conversationDetail Models.ConversationDetail, conversationDetail Models.ConversationDetail,
associationKey uuid.UUID, associationKey uuid.UUID,
admin bool, admin bool,
key aesKey,
key AesKey,
) (Models.ConversationDetailUser, error) { ) (Models.ConversationDetailUser, error) {
var ( var (
conversationDetailUser Models.ConversationDetailUser conversationDetailUser Models.ConversationDetailUser
@ -224,7 +224,7 @@ func seedConversationDetailUser(
func SeedMessages() { func SeedMessages() {
var ( var (
conversationDetail Models.ConversationDetail conversationDetail Models.ConversationDetail
key aesKey
key AesKey
primaryUser Models.User primaryUser Models.User
primaryUserAssociationKey uuid.UUID primaryUserAssociationKey uuid.UUID
secondaryUser Models.User secondaryUser Models.User


+ 1
- 1
Backend/Database/Seeder/UserSeeder.go View File

@ -25,7 +25,7 @@ var userNames = []string{
func createUser(username string) (Models.User, error) { func createUser(username string) (Models.User, error) {
var ( var (
userData Models.User userData Models.User
userKey aesKey
userKey AesKey
password string password string
err error err error
) )


+ 9
- 9
Backend/Database/Seeder/encryption.go View File

@ -17,12 +17,12 @@ import (
"golang.org/x/crypto/pbkdf2" "golang.org/x/crypto/pbkdf2"
) )
type aesKey struct {
type AesKey struct {
Key []byte Key []byte
Iv []byte Iv []byte
} }
func (key aesKey) encode() string {
func (key AesKey) encode() string {
return base64.StdEncoding.EncodeToString(key.Key) return base64.StdEncoding.EncodeToString(key.Key)
} }
@ -71,7 +71,7 @@ func pkcs7strip(data []byte, blockSize int) ([]byte, error) {
return data[:length-padLen], nil return data[:length-padLen], nil
} }
func GenerateAesKey() (aesKey, error) {
func GenerateAesKey() (AesKey, error) {
var ( var (
saltBytes []byte = []byte{} saltBytes []byte = []byte{}
password []byte password []byte
@ -83,22 +83,22 @@ func GenerateAesKey() (aesKey, error) {
password = make([]byte, 64) password = make([]byte, 64)
_, err = rand.Read(password) _, err = rand.Read(password)
if err != nil { if err != nil {
return aesKey{}, err
return AesKey{}, err
} }
seed = make([]byte, 64) seed = make([]byte, 64)
_, err = rand.Read(seed) _, err = rand.Read(seed)
if err != nil { if err != nil {
return aesKey{}, err
return AesKey{}, err
} }
iv = make([]byte, 16) iv = make([]byte, 16)
_, err = rand.Read(iv) _, err = rand.Read(iv)
if err != nil { if err != nil {
return aesKey{}, err
return AesKey{}, err
} }
return aesKey{
return AesKey{
Key: pbkdf2.Key( Key: pbkdf2.Key(
password, password,
saltBytes, saltBytes,
@ -110,7 +110,7 @@ func GenerateAesKey() (aesKey, error) {
}, nil }, nil
} }
func (key aesKey) AesEncrypt(plaintext []byte) ([]byte, error) {
func (key AesKey) AesEncrypt(plaintext []byte) ([]byte, error) {
var ( var (
bPlaintext []byte bPlaintext []byte
ciphertext []byte ciphertext []byte
@ -134,7 +134,7 @@ func (key aesKey) AesEncrypt(plaintext []byte) ([]byte, error) {
return ciphertext, nil return ciphertext, nil
} }
func (key aesKey) AesDecrypt(ciphertext []byte) ([]byte, error) {
func (key AesKey) AesDecrypt(ciphertext []byte) ([]byte, error) {
var ( var (
plaintext []byte plaintext []byte
iv []byte iv []byte


Loading…
Cancel
Save