Browse Source

Add new tests

pull/5/head
Tovi Jaeschke-Rogers 2 years ago
parent
commit
9a29a9d82c
5 changed files with 349 additions and 68 deletions
  1. +137
    -0
      Backend/Api/Friends/AcceptFriendRequest_test.go
  2. +204
    -0
      Backend/Api/Friends/CreateFriendRequest_test.go
  3. +0
    -60
      Backend/Api/Friends/FriendRequest.go
  4. +6
    -6
      Backend/Api/Friends/Friends_test.go
  5. +2
    -2
      mobile/lib/views/authentication/login.dart

+ 137
- 0
Backend/Api/Friends/AcceptFriendRequest_test.go View File

@ -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
}
}
}

+ 204
- 0
Backend/Api/Friends/CreateFriendRequest_test.go View File

@ -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
}
}

+ 0
- 60
Backend/Api/Friends/FriendRequest.go View File

@ -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)
}

+ 6
- 6
Backend/Api/Friends/Friends_test.go View File

@ -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())


+ 2
- 2
mobile/lib/views/authentication/login.dart View File

@ -87,8 +87,8 @@ class _LoginWidgetState extends State<LoginWidget> {
);
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(


Loading…
Cancel
Save