|
|
- package Friends_test
-
- import (
- "bytes"
- "encoding/base64"
- "encoding/json"
- "fmt"
- "net/http"
- "testing"
-
- "git.tovijaeschke.xyz/tovi/Envelope/Backend/Database"
- "git.tovijaeschke.xyz/tovi/Envelope/Backend/Database/Seeder"
- "git.tovijaeschke.xyz/tovi/Envelope/Backend/Models"
- "git.tovijaeschke.xyz/tovi/Envelope/Backend/Tests"
- )
-
- func Test_AcceptFriendRequest(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")
- if err != nil {
- t.Errorf("Expected nil, recieved %s", err.Error())
- return
- }
-
- u2, err := Tests.InitTestCreateUser("test2")
- 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
- }
-
- decodedPublicKey := Seeder.GetPubKey()
-
- encPublicKey, err := key.AesEncrypt([]byte(Seeder.PublicKey))
- if err != nil {
- t.Errorf("Expected nil, recieved %s", err.Error())
- return
- }
-
- friendReq := Models.FriendRequest{
- UserID: u.ID,
- FriendID: base64.StdEncoding.EncodeToString(
- Seeder.EncryptWithPublicKey(
- []byte(u2.ID.String()),
- decodedPublicKey,
- ),
- ),
- FriendUsername: base64.StdEncoding.EncodeToString(
- Seeder.EncryptWithPublicKey(
- []byte(u2.Username),
- decodedPublicKey,
- ),
- ),
- FriendPublicAsymmetricKey: base64.StdEncoding.EncodeToString(
- encPublicKey,
- ),
- SymmetricKey: base64.StdEncoding.EncodeToString(
- Seeder.EncryptWithPublicKey(key.Key, decodedPublicKey),
- ),
- }
-
- err = Database.CreateFriendRequest(&friendReq)
- if err != nil {
- t.Errorf("Expected nil, recieved %s", err.Error())
- return
- }
-
- friendReqResponse := Models.FriendRequest{
- UserID: u2.ID,
- FriendID: base64.StdEncoding.EncodeToString(
- Seeder.EncryptWithPublicKey(
- []byte(u.ID.String()),
- decodedPublicKey,
- ),
- ),
- FriendUsername: base64.StdEncoding.EncodeToString(
- Seeder.EncryptWithPublicKey(
- []byte(u.Username),
- decodedPublicKey,
- ),
- ),
- FriendPublicAsymmetricKey: base64.StdEncoding.EncodeToString(
- encPublicKey,
- ),
- SymmetricKey: base64.StdEncoding.EncodeToString(
- Seeder.EncryptWithPublicKey(key.Key, decodedPublicKey),
- ),
- }
-
- jsonStr, _ := json.Marshal(friendReqResponse)
- req, _ := http.NewRequest(
- "POST",
- fmt.Sprintf(
- "%s/api/v1/auth/friend_request/%s",
- ts.URL,
- friendReq.ID,
- ),
- 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)
- return
- }
-
- var reqs []Models.FriendRequest
-
- err = Database.DB.Find(&reqs).Error
- if err != nil {
- t.Errorf("Expected nil, recieved %s", err.Error())
- return
- }
-
- for _, r := range reqs {
- if r.AcceptedAt.Valid != true {
- t.Errorf("Expected true, recieved false")
- return
- }
- }
- }
|