@ -0,0 +1,87 @@ | |||||
package Friends | |||||
import ( | |||||
"encoding/json" | |||||
"io/ioutil" | |||||
"net/http" | |||||
"time" | |||||
"git.tovijaeschke.xyz/tovi/Capsule/Backend/Database" | |||||
"git.tovijaeschke.xyz/tovi/Capsule/Backend/Models" | |||||
) | |||||
// CreateFriendRequest creates a FriendRequest from post data | |||||
func CreateFriendRequest(w http.ResponseWriter, r *http.Request) { | |||||
var ( | |||||
friendRequest Models.FriendRequest | |||||
requestBody []byte | |||||
returnJSON []byte | |||||
err error | |||||
) | |||||
requestBody, err = ioutil.ReadAll(r.Body) | |||||
if err != nil { | |||||
http.Error(w, "Error", http.StatusInternalServerError) | |||||
return | |||||
} | |||||
err = json.Unmarshal(requestBody, &friendRequest) | |||||
if err != nil { | |||||
http.Error(w, "Error", http.StatusInternalServerError) | |||||
return | |||||
} | |||||
friendRequest.AcceptedAt.Scan(nil) | |||||
err = Database.CreateFriendRequest(&friendRequest) | |||||
if err != nil { | |||||
http.Error(w, "Error", http.StatusInternalServerError) | |||||
return | |||||
} | |||||
returnJSON, err = json.MarshalIndent(friendRequest, "", " ") | |||||
if err != nil { | |||||
http.Error(w, "Error", http.StatusInternalServerError) | |||||
return | |||||
} | |||||
// Return updated json | |||||
w.WriteHeader(http.StatusOK) | |||||
w.Write(returnJSON) | |||||
} | |||||
// CreateFriendRequestQrCode creates a FriendRequest from post data from qr code scan | |||||
func CreateFriendRequestQrCode(w http.ResponseWriter, r *http.Request) { | |||||
var ( | |||||
friendRequests []Models.FriendRequest | |||||
requestBody []byte | |||||
i int | |||||
err error | |||||
) | |||||
requestBody, err = ioutil.ReadAll(r.Body) | |||||
if err != nil { | |||||
http.Error(w, "Error", http.StatusInternalServerError) | |||||
return | |||||
} | |||||
err = json.Unmarshal(requestBody, &friendRequests) | |||||
if err != nil { | |||||
http.Error(w, "Error", http.StatusInternalServerError) | |||||
return | |||||
} | |||||
for i = range friendRequests { | |||||
friendRequests[i].AcceptedAt.Time = time.Now() | |||||
friendRequests[i].AcceptedAt.Valid = true | |||||
} | |||||
err = Database.CreateFriendRequests(&friendRequests) | |||||
if err != nil { | |||||
http.Error(w, "Error", http.StatusInternalServerError) | |||||
return | |||||
} | |||||
// Return updated json | |||||
w.WriteHeader(http.StatusOK) | |||||
} |
@ -1,41 +0,0 @@ | |||||
package Friends | |||||
import ( | |||||
"encoding/json" | |||||
"net/http" | |||||
"git.tovijaeschke.xyz/tovi/Capsule/Backend/Api/Auth" | |||||
"git.tovijaeschke.xyz/tovi/Capsule/Backend/Database" | |||||
"git.tovijaeschke.xyz/tovi/Capsule/Backend/Models" | |||||
) | |||||
// FriendRequestList gets friend request list | |||||
func FriendRequestList(w http.ResponseWriter, r *http.Request) { | |||||
var ( | |||||
userSession Models.Session | |||||
friends []Models.FriendRequest | |||||
returnJSON []byte | |||||
err error | |||||
) | |||||
userSession, err = Auth.CheckCookie(r) | |||||
if err != nil { | |||||
http.Error(w, "Forbidden", http.StatusUnauthorized) | |||||
return | |||||
} | |||||
friends, err = Database.GetFriendRequestsByUserID(userSession.UserID.String()) | |||||
if err != nil { | |||||
http.Error(w, "Error", http.StatusInternalServerError) | |||||
return | |||||
} | |||||
returnJSON, err = json.MarshalIndent(friends, "", " ") | |||||
if err != nil { | |||||
http.Error(w, "Error", http.StatusInternalServerError) | |||||
return | |||||
} | |||||
w.WriteHeader(http.StatusOK) | |||||
w.Write(returnJSON) | |||||
} |
@ -0,0 +1,124 @@ | |||||
package Friends_test | |||||
import ( | |||||
"encoding/base64" | |||||
"encoding/json" | |||||
"fmt" | |||||
"io/ioutil" | |||||
"net/http" | |||||
"testing" | |||||
"time" | |||||
"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_FriendRequestList(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 | |||||
} | |||||
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()) | |||||
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), | |||||
), | |||||
} | |||||
if i > 20 { | |||||
friendReq.AcceptedAt.Time = time.Now() | |||||
friendReq.AcceptedAt.Valid = true | |||||
} | |||||
err = Database.CreateFriendRequest(&friendReq) | |||||
if err != nil { | |||||
t.Errorf("Expected nil, recieved %s", err.Error()) | |||||
return | |||||
} | |||||
} | |||||
req, _ := http.NewRequest("GET", ts.URL+"/api/v1/auth/friend_requests", nil) | |||||
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 | |||||
} | |||||
requestBody, err := ioutil.ReadAll(resp.Body) | |||||
if err != nil { | |||||
t.Errorf("Expected %d, recieved %d", http.StatusOK, resp.StatusCode) | |||||
return | |||||
} | |||||
var users []Models.FriendRequest | |||||
json.Unmarshal(requestBody, &users) | |||||
if len(users) != 20 { | |||||
t.Errorf("Expected %d, recieved %d", 1, len(users)) | |||||
return | |||||
} | |||||
for i := 0; i < 20; i++ { | |||||
eq := true | |||||
if i > 8 { | |||||
eq = false | |||||
} | |||||
if users[i].AcceptedAt.Valid != eq { | |||||
t.Errorf( | |||||
"Expected %v, recieved %v, on user %d", | |||||
eq, users[i].AcceptedAt.Valid, | |||||
i, | |||||
) | |||||
return | |||||
} | |||||
} | |||||
} |
@ -0,0 +1,106 @@ | |||||
package Users_test | |||||
import ( | |||||
"encoding/json" | |||||
"fmt" | |||||
"io/ioutil" | |||||
"net/http" | |||||
"testing" | |||||
"git.tovijaeschke.xyz/tovi/Capsule/Backend/Models" | |||||
"git.tovijaeschke.xyz/tovi/Capsule/Backend/Tests" | |||||
) | |||||
func Test_SearchUsers(t *testing.T) { | |||||
client, ts, err := Tests.InitTestEnv() | |||||
if err != nil { | |||||
t.Errorf("Expected nil, recieved %s", err.Error()) | |||||
return | |||||
} | |||||
u2, err := Tests.InitTestCreateUser("abcd") | |||||
req, _ := http.NewRequest( | |||||
"GET", | |||||
fmt.Sprintf("%s/api/v1/auth/users?username=%s", ts.URL, u2.Username), | |||||
nil, | |||||
) | |||||
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 | |||||
} | |||||
requestBody, err := ioutil.ReadAll(resp.Body) | |||||
if err != nil { | |||||
t.Errorf("Expected %d, recieved %d", http.StatusOK, resp.StatusCode) | |||||
return | |||||
} | |||||
var user Models.User | |||||
json.Unmarshal(requestBody, &user) | |||||
if user.Username != "abcd" { | |||||
t.Errorf("Expected abcd, recieved %s", user.Username) | |||||
return | |||||
} | |||||
if user.Password != "" { | |||||
t.Errorf("Expected \"\", recieved %s", user.Password) | |||||
return | |||||
} | |||||
if user.AsymmetricPrivateKey != "" { | |||||
t.Errorf("Expected \"\", recieved %s", user.AsymmetricPrivateKey) | |||||
return | |||||
} | |||||
} | |||||
func Test_SearchUsersPartialMatchFails(t *testing.T) { | |||||
client, ts, err := Tests.InitTestEnv() | |||||
if err != nil { | |||||
t.Errorf("Expected nil, recieved %s", err.Error()) | |||||
return | |||||
} | |||||
_, err = Tests.InitTestCreateUser("abcd") | |||||
req, _ := http.NewRequest( | |||||
"GET", | |||||
fmt.Sprintf("%s/api/v1/auth/users?username=%s", ts.URL, "abc"), | |||||
nil, | |||||
) | |||||
resp, err := client.Do(req) | |||||
if err != nil { | |||||
t.Errorf("Expected nil, recieved %s", err.Error()) | |||||
return | |||||
} | |||||
if resp.StatusCode != http.StatusNotFound { | |||||
t.Errorf("Expected %d, recieved %d", http.StatusOK, resp.StatusCode) | |||||
return | |||||
} | |||||
requestBody, err := ioutil.ReadAll(resp.Body) | |||||
if err != nil { | |||||
t.Errorf("Expected %d, recieved %d", http.StatusOK, resp.StatusCode) | |||||
return | |||||
} | |||||
var user interface{} | |||||
json.Unmarshal(requestBody, &user) | |||||
if user != nil { | |||||
t.Errorf("Expected nil, recieved %+v", user) | |||||
return | |||||
} | |||||
} |