From 9a29a9d82c322eb41076c5dab4ba42d6ec3d8238 Mon Sep 17 00:00:00 2001 From: Tovi Jaeschke-Rogers Date: Fri, 7 Oct 2022 16:54:20 +1030 Subject: [PATCH] Add new tests --- .../Api/Friends/AcceptFriendRequest_test.go | 137 ++++++++++++ .../Api/Friends/CreateFriendRequest_test.go | 204 ++++++++++++++++++ Backend/Api/Friends/FriendRequest.go | 60 ------ Backend/Api/Friends/Friends_test.go | 12 +- mobile/lib/views/authentication/login.dart | 4 +- 5 files changed, 349 insertions(+), 68 deletions(-) create mode 100644 Backend/Api/Friends/AcceptFriendRequest_test.go create mode 100644 Backend/Api/Friends/CreateFriendRequest_test.go delete mode 100644 Backend/Api/Friends/FriendRequest.go diff --git a/Backend/Api/Friends/AcceptFriendRequest_test.go b/Backend/Api/Friends/AcceptFriendRequest_test.go new file mode 100644 index 0000000..ff5c642 --- /dev/null +++ b/Backend/Api/Friends/AcceptFriendRequest_test.go @@ -0,0 +1,137 @@ +package Friends_test + +import ( + "bytes" + "encoding/base64" + "encoding/json" + "fmt" + "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_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 + } + } +} diff --git a/Backend/Api/Friends/CreateFriendRequest_test.go b/Backend/Api/Friends/CreateFriendRequest_test.go new file mode 100644 index 0000000..49b152e --- /dev/null +++ b/Backend/Api/Friends/CreateFriendRequest_test.go @@ -0,0 +1,204 @@ +package Friends_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 Test_CreateFriendRequest(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), + ), + } + + jsonStr, _ := json.Marshal(friendReq) + req, _ := http.NewRequest("POST", ts.URL+"/api/v1/auth/friend_request", 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.StatusOK { + t.Errorf("Expected %d, recieved %d", http.StatusOK, resp.StatusCode) + return + } + + var r Models.FriendRequest + + err = Database.DB.First(&r).Error + if err != nil { + t.Errorf("Expected nil, recieved %s", err.Error()) + return + } + + if r.AcceptedAt.Valid == true { + t.Errorf("Expected false, recieved true") + return + } +} + +func Test_CreateFriendRequestQrCode(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), + ), + } + + friendReq2 := 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([]Models.FriendRequest{friendReq, friendReq2}) + req, _ := http.NewRequest("POST", ts.URL+"/api/v1/auth/friend_request/qr_code", 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.StatusOK { + t.Errorf("Expected %d, recieved %d", http.StatusOK, resp.StatusCode) + return + } + + var r Models.FriendRequest + + err = Database.DB.First(&r).Error + if err != nil { + t.Errorf("Expected nil, recieved %s", err.Error()) + return + } + + if r.AcceptedAt.Valid == false { + t.Errorf("Expected true, recieved false") + return + } +} diff --git a/Backend/Api/Friends/FriendRequest.go b/Backend/Api/Friends/FriendRequest.go deleted file mode 100644 index c704800..0000000 --- a/Backend/Api/Friends/FriendRequest.go +++ /dev/null @@ -1,60 +0,0 @@ -package Friends - -import ( - "encoding/json" - "io/ioutil" - "net/http" - - "git.tovijaeschke.xyz/tovi/Capsule/Backend/Database" - "git.tovijaeschke.xyz/tovi/Capsule/Backend/Models" - "git.tovijaeschke.xyz/tovi/Capsule/Backend/Util" -) - -func FriendRequest(w http.ResponseWriter, r *http.Request) { - var ( - user Models.User - requestBody []byte - requestJson map[string]interface{} - friendID string - friendRequest Models.FriendRequest - ok bool - err error - ) - - user, err = Util.GetUserById(w, r) - if err != nil { - http.Error(w, "Not Found", http.StatusNotFound) - return - } - - requestBody, err = ioutil.ReadAll(r.Body) - if err != nil { - http.Error(w, "Not Found", http.StatusNotFound) - return - } - - json.Unmarshal(requestBody, &requestJson) - if requestJson["id"] == nil { - http.Error(w, "Invalid Data", http.StatusBadRequest) - return - } - - friendID, ok = requestJson["id"].(string) - if !ok { - http.Error(w, "Error", http.StatusInternalServerError) - return - } - - friendRequest = Models.FriendRequest{ - UserID: user.ID, - FriendID: friendID, - } - - err = Database.CreateFriendRequest(&friendRequest) - if requestJson["id"] == nil { - http.Error(w, "Error", http.StatusInternalServerError) - return - } - - w.WriteHeader(http.StatusNoContent) -} diff --git a/Backend/Api/Friends/Friends_test.go b/Backend/Api/Friends/Friends_test.go index d18de12..ee177b8 100644 --- a/Backend/Api/Friends/Friends_test.go +++ b/Backend/Api/Friends/Friends_test.go @@ -28,17 +28,17 @@ func Test_FriendRequestList(t *testing.T) { return } + key, err := Seeder.GenerateAesKey() + if err != nil { + t.Errorf("Expected nil, recieved %s", err.Error()) + return + } + for i := 0; i < 30; i++ { u2, err := Tests.InitTestCreateUser(fmt.Sprintf("test%d", i)) decodedPublicKey := Seeder.GetPubKey() - key, err := Seeder.GenerateAesKey() - if err != nil { - t.Errorf("Expected nil, recieved %s", err.Error()) - return - } - encPublicKey, err := key.AesEncrypt([]byte(Seeder.PublicKey)) if err != nil { t.Errorf("Expected nil, recieved %s", err.Error()) diff --git a/mobile/lib/views/authentication/login.dart b/mobile/lib/views/authentication/login.dart index d4c56a0..44788b3 100644 --- a/mobile/lib/views/authentication/login.dart +++ b/mobile/lib/views/authentication/login.dart @@ -87,8 +87,8 @@ class _LoginWidgetState extends State { ); final ButtonStyle buttonStyle = ElevatedButton.styleFrom( - primary: Theme.of(context).colorScheme.surface, - onPrimary: Theme.of(context).colorScheme.onSurface, + backgroundColor: Theme.of(context).colorScheme.surface, + foregroundColor: Theme.of(context).colorScheme.onSurface, minimumSize: const Size.fromHeight(50), padding: const EdgeInsets.symmetric(horizontal: 10, vertical: 10), textStyle: TextStyle(