Skip to content

Commit

Permalink
fix tests with changes
Browse files Browse the repository at this point in the history
  • Loading branch information
akrck02 committed Jul 26, 2024
1 parent 53188ef commit ee3ca29
Show file tree
Hide file tree
Showing 10 changed files with 67 additions and 81 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ go 1.22
toolchain go1.22.3

require (
github.com/akrck02/valhalla-core-sdk v0.0.14
github.com/akrck02/valhalla-core-sdk v0.0.15-20240726224904-dev
github.com/golang-jwt/jwt/v5 v5.2.1
github.com/joho/godotenv v1.5.1
go.mongodb.org/mongo-driver v1.15.0
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
github.com/akrck02/valhalla-core-sdk v0.0.14 h1:Q5JReoskRV2Gy1hFeexAqRnSurc2Y0L7HZEvssgRWEs=
github.com/akrck02/valhalla-core-sdk v0.0.14/go.mod h1:qBovw83Vo/iXggXZS4ttcMqF9xFSR5ogu7G+RWh/jGg=
github.com/akrck02/valhalla-core-sdk v0.0.15-20240726224904-dev h1:Jmrin6Njq1JoEZ+YHWNpAZjTOU6OLfztqD5Vy2fjxVk=
github.com/akrck02/valhalla-core-sdk v0.0.15-20240726224904-dev/go.mod h1:qBovw83Vo/iXggXZS4ttcMqF9xFSR5ogu7G+RWh/jGg=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/golang-jwt/jwt/v5 v5.2.1 h1:OuVbFODueb089Lh128TAcimifWaLhJwVflnrgM17wHk=
Expand Down
10 changes: 5 additions & 5 deletions services/permission/permission.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,22 +8,22 @@ import (
// Users

func CanEditUser(author *usersmodels.User, user *usersmodels.User) bool {
return author.ID == user.ID
return author.Id == user.Id
}

func CanSeeUser(author *usersmodels.User, user *usersmodels.User) bool {
return author.ID == user.ID
return author.Id == user.Id
}
func CanDeleteProject(logedUser *usersmodels.User, project *projectmodels.Project) bool {
return logedUser.ID == project.Owner
return logedUser.Id == project.Owner
}

//Projects

func CanEditProject(user *usersmodels.User, project *projectmodels.Project) bool {
return user.ID == project.Owner
return user.Id == project.Owner
}

func CanSeeProject(user *usersmodels.User, project *projectmodels.Project) bool {
return user.ID == project.Owner
return user.Id == project.Owner
}
42 changes: 22 additions & 20 deletions services/project/project.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,13 +48,13 @@ func CreateProject(conn *mongo.Client, project *projectmodels.Project) (*project
}

// Check if the user exists and is valid
owner, userGetError := userdal.GetUser(conn, &usersmodels.User{ID: project.Owner}, true)
owner, userGetError := userdal.GetUser(conn, &usersmodels.User{Id: project.Owner}, true)
if userGetError != nil {
return nil, userGetError
}

// convert the owner ID to an object ID
_, parsingError := utils.StringToObjectId(owner.ID)
// convert the owner ID to an object Id
_, parsingError := utils.StringToObjectId(owner.Id)
if parsingError != nil {
return nil, &apimodels.Error{
Status: http.StatusBadRequest,
Expand All @@ -66,7 +66,7 @@ func CreateProject(conn *mongo.Client, project *projectmodels.Project) (*project
// Check if the project already exists
// it is not possible to have two projects with the same name and owner
coll := conn.Database(database.CurrentDatabase).Collection(database.PROJECT)
found := projectNameExists(coll, project.Name, owner.ID)
found := projectNameExistsForOwner(coll, project.Name, owner.Id)

if found {
return nil, &apimodels.Error{
Expand Down Expand Up @@ -99,32 +99,26 @@ func CreateProject(conn *mongo.Client, project *projectmodels.Project) (*project
}

// Get the project id and return the created project
project.ID = insertResult.InsertedID.(primitive.ObjectID).Hex()
project.Id = insertResult.InsertedID.(primitive.ObjectID).Hex()
return project, nil
}

func EditProject(conn *mongo.Client, project *projectmodels.Project) *apimodels.Error {

// Transform team id to object id
// also check if team id is valid
objID, parsingError := utils.StringToObjectId(project.ID)
objID, parsingError := utils.StringToObjectId(project.Id)

if parsingError != nil {
return &apimodels.Error{
Status: http.StatusBadRequest,
Error: apierror.InvalidObjectId,
Message: "Invalid object id [" + project.ID + "] : " + parsingError.Error(),
Message: "Invalid object id [" + project.Id + "] : " + parsingError.Error(),
}
}

coll := conn.Database(database.CurrentDatabase).Collection(database.PROJECT)
updateResult, updateError := coll.UpdateByID(database.GetDefaultContext(), objID, bson.M{
"$set": bson.M{
"name": project.Name,
"description": project.Description,
"updatedate": project.LastUpdate,
},
})
updateResult, updateError := coll.UpdateByID(database.GetDefaultContext(), objID, bson.M{"$set": project.Bson(true)})

// Check if team was updated
if updateError != nil {
Expand Down Expand Up @@ -161,12 +155,12 @@ func DeleteProject(conn *mongo.Client, project *projectmodels.Project) *apimodel
}

// Check if the project ID is valid
id, parsingError := utils.StringToObjectId(project.ID)
id, parsingError := utils.StringToObjectId(project.Id)
if parsingError != nil {
return &apimodels.Error{
Status: http.StatusBadRequest,
Error: apierror.InvalidObjectId,
Message: "Invalid object id [" + project.ID + "] : " + parsingError.Error(),
Message: "Invalid object id [" + project.Id + "] : " + parsingError.Error(),
}
}

Expand Down Expand Up @@ -198,18 +192,20 @@ func DeleteProject(conn *mongo.Client, project *projectmodels.Project) *apimodel
func GetProject(conn *mongo.Client, project *projectmodels.Project) (*projectmodels.Project, *apimodels.Error) {

// Check if the project ID is valid
projectIdObject, parsingError := utils.StringToObjectId(project.ID)
projectIdObject, parsingError := utils.StringToObjectId(project.Id)
if parsingError != nil {
return nil, &apimodels.Error{
Status: http.StatusBadRequest,
Error: apierror.InvalidObjectId,
Message: "Invalid object id [" + project.ID + "] : " + parsingError.Error(),
Message: "Invalid object id [" + project.Id + "] : " + parsingError.Error(),
}
}

// Get project by id
projects := conn.Database(database.CurrentDatabase).Collection(database.PROJECT)
found := projectmodels.Project{}
found.Default()

err := projects.FindOne(database.GetDefaultContext(), bson.M{"_id": projectIdObject}).Decode(&found)

// If an error occurs, return the error
Expand All @@ -222,7 +218,7 @@ func GetProject(conn *mongo.Client, project *projectmodels.Project) (*projectmod
}

// If the project is not found, return an error
if utils.IsEmpty(found.ID) {
if utils.IsEmpty(found.Id) {
return nil, &apimodels.Error{
Status: http.StatusNotFound,
Error: apierror.ProjectNotFound,
Expand All @@ -249,13 +245,19 @@ func GetUserProjects(conn *mongo.Client, ownerId string) []projectmodels.Project
return nil
}

// Get all projects
var result []projectmodels.Project
cursor.All(database.GetDefaultContext(), &result)

// set default values for each project
for i := 0; i < len(result); i++ {
result[i].Default()
}

return result
}

func projectNameExists(coll *mongo.Collection, name string, owner string) bool {
func projectNameExistsForOwner(coll *mongo.Collection, name string, owner string) bool {
filter := bson.M{"name": name, "owner": owner}

var result *projectmodels.Project
Expand Down
23 changes: 9 additions & 14 deletions services/team/team.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,15 +98,15 @@ func CreateTeam(conn *mongo.Client, team *teammodels.Team) *apimodels.Error {
}
}

team.ID = res.InsertedID.(primitive.ObjectID).Hex()
team.Id = res.InsertedID.(primitive.ObjectID).Hex()
return nil
}

func DeleteTeam(conn *mongo.Client, team *teammodels.Team) *apimodels.Error {

// Transform team id to object id
// also check if team id is valid
objID, err := utils.StringToObjectId(team.ID)
objID, err := utils.StringToObjectId(team.Id)

if err != nil {
return &apimodels.Error{
Expand Down Expand Up @@ -136,7 +136,7 @@ func EditTeam(conn *mongo.Client, team *teammodels.Team) *apimodels.Error {

// Transform team id to object id
// also check if team id is valid
objID, err := utils.StringToObjectId(team.ID)
objID, err := utils.StringToObjectId(team.Id)

if err != nil {
return &apimodels.Error{
Expand All @@ -151,12 +151,7 @@ func EditTeam(conn *mongo.Client, team *teammodels.Team) *apimodels.Error {
lastUpdate := utils.GetCurrentMillis()
team.LastUpdate = &lastUpdate
_, err = coll.UpdateOne(database.GetDefaultContext(), bson.M{"_id": objID}, bson.M{
"$set": bson.M{
"name": team.Name,
"description": team.Description,
"profilepic": team.ProfilePic,
"updatedate": team.LastUpdate,
},
"$set": team.Bson(true),
})

// Check if team was updated
Expand Down Expand Up @@ -184,7 +179,7 @@ func EditTeamOwner(conn *mongo.Client, team *teammodels.Team) *apimodels.Error {

// Transform team id to object id
// also check if team id is valid
objID, err1 := utils.StringToObjectId(team.ID)
objID, err1 := utils.StringToObjectId(team.Id)

if err1 != nil {
return &apimodels.Error{
Expand Down Expand Up @@ -383,7 +378,7 @@ func GetTeams(conn *mongo.Client, user *usersmodels.User) ([]*teammodels.Team, *

// Get the teams that the user owns
coll := conn.Database(database.CurrentDatabase).Collection(database.TEAM)
teamsCursor, err := coll.Find(database.GetDefaultContext(), bson.M{"owner": user.ID})
teamsCursor, err := coll.Find(database.GetDefaultContext(), bson.M{"owner": user.Id})

if err != nil {
return nil, &apimodels.Error{
Expand Down Expand Up @@ -415,7 +410,7 @@ func GetTeams(conn *mongo.Client, user *usersmodels.User) ([]*teammodels.Team, *

func GetTeam(conn *mongo.Client, team *teammodels.Team) (*teammodels.Team, *apimodels.Error) {

objID, err1 := utils.StringToObjectId(team.ID)
objID, err1 := utils.StringToObjectId(team.Id)

if err1 != nil {
return nil, &apimodels.Error{
Expand Down Expand Up @@ -507,13 +502,13 @@ func isUserMemberOrOwner(conn *mongo.Client, request *MemberChangeRequest) bool

err := coll.FindOne(database.GetDefaultContext(), filterMember).Decode(&result)

if err == nil && result.ID != "" {
if err == nil && result.Id != "" {
return true
}

err = coll.FindOne(database.GetDefaultContext(), filterOwner).Decode(&result)

if err == nil && result.ID != "" {
if err == nil && result.Id != "" {
return true
}

Expand Down
45 changes: 16 additions & 29 deletions services/user/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ func Register(conn *mongo.Client, user *usersmodels.User) *apimodels.Error {
}

// get user from database
user.ID = res.InsertedID.(primitive.ObjectID).Hex()
user.Id = res.InsertedID.(primitive.ObjectID).Hex()
user.CreationDate = userToInsert.CreationDate
user.LastUpdate = userToInsert.LastUpdate
user.ValidationCode = userToInsert.ValidationCode
Expand Down Expand Up @@ -176,7 +176,7 @@ func EditUser(conn *mongo.Client, user *usersmodels.User) *apimodels.Error {

users := conn.Database(database.CurrentDatabase).Collection(database.USER)

if utils.IsEmpty(user.ID) {
if utils.IsEmpty(user.Id) {
return &apimodels.Error{
Status: http.StatusBadRequest,
Error: apierror.EmptyUsername,
Expand All @@ -185,7 +185,7 @@ func EditUser(conn *mongo.Client, user *usersmodels.User) *apimodels.Error {
}

// Get if id is valid
_, parseIdError := utils.StringToObjectId(user.ID)
_, parseIdError := utils.StringToObjectId(user.Id)

if parseIdError != nil {
return &apimodels.Error{
Expand All @@ -196,7 +196,7 @@ func EditUser(conn *mongo.Client, user *usersmodels.User) *apimodels.Error {
}

// Check if the user exists
found := userExists(user.ID, users)
found := userExists(user.Id, users)

if found == nil {
return &apimodels.Error{
Expand All @@ -223,28 +223,15 @@ func EditUser(conn *mongo.Client, user *usersmodels.User) *apimodels.Error {
if checkedError != nil {
return checkedError
}
}

setObject := bson.M{}

if user.Username != "" {
setObject["username"] = user.Username
}

if user.Password != "" {
encryptedPass := user.Password
setObject["password"] = utils.EncryptSha256(encryptedPass)
}

if user.ProfilePic != "" {
setObject["profilePic"] = user.ProfilePic
user.Password = utils.EncryptSha256(user.Password)
}

setObject["updatedate"] = utils.GetCurrentMillis()
toUpdate := bson.M{"$set": setObject}
lastUpdate := utils.GetCurrentMillis()
user.LastUpdate = &lastUpdate

// update user on database
objID, parseObjectIdError := primitive.ObjectIDFromHex(user.ID)
objID, parseObjectIdError := primitive.ObjectIDFromHex(user.Id)

if parseObjectIdError != nil {
return &apimodels.Error{
Expand All @@ -254,13 +241,13 @@ func EditUser(conn *mongo.Client, user *usersmodels.User) *apimodels.Error {
}
}

res, err := users.UpdateByID(database.GetDefaultContext(), objID, toUpdate)
res, err := users.UpdateByID(database.GetDefaultContext(), objID, bson.M{"$set": user.Bson(true)})

if err != nil {
return &apimodels.Error{
Status: http.StatusBadRequest,
Error: apierror.DatabaseError,
Message: "User not updated",
Message: "User not updated: " + err.Error(),
}
}

Expand Down Expand Up @@ -377,7 +364,7 @@ func EditUserEmail(conn *mongo.Client, mail *EmailChangeRequest) *apimodels.Erro

func EditUserProfilePicture(conn *mongo.Client, user *usersmodels.User, picture []byte) *apimodels.Error {

if utils.IsEmpty(user.ID) {
if utils.IsEmpty(user.Id) {
return &apimodels.Error{
Status: http.StatusBadRequest,
Error: apierror.EmptyUsername,
Expand All @@ -386,7 +373,7 @@ func EditUserProfilePicture(conn *mongo.Client, user *usersmodels.User, picture
}

// Get if id is valid
_, parseIdError := utils.StringToObjectId(user.ID)
_, parseIdError := utils.StringToObjectId(user.Id)

if parseIdError != nil {
return &apimodels.Error{
Expand All @@ -397,7 +384,7 @@ func EditUserProfilePicture(conn *mongo.Client, user *usersmodels.User, picture
}

//if the path base profile pic does not exist, create it
var profilePathDir = utils.GetProfilePicturePath(user.ID, configuration.PROFILE_PICTURES_PATH)
var profilePathDir = utils.GetProfilePicturePath(user.Id, configuration.PROFILE_PICTURES_PATH)
if !utils.ExistsDir(profilePathDir) {
err := utils.CreateDir(profilePathDir)

Expand Down Expand Up @@ -499,7 +486,7 @@ func DeleteUser(conn *mongo.Client, user *usersmodels.User) *apimodels.Error {

func GetUser(conn *mongo.Client, user *usersmodels.User, secure bool) (*usersmodels.User, *apimodels.Error) {

id, err := utils.StringToObjectId(user.ID)
id, err := utils.StringToObjectId(user.Id)

if err != nil {
return nil, &apimodels.Error{
Expand All @@ -526,7 +513,7 @@ func GetUser(conn *mongo.Client, user *usersmodels.User, secure bool) (*usersmod
}

// if user not found, return error
if found.ID == "" {
if found.Id == "" {
return nil, &apimodels.Error{
Status: http.StatusNotFound,
Error: apierror.UserNotFound,
Expand Down Expand Up @@ -562,7 +549,7 @@ func GetUserByEmail(conn *mongo.Client, email string, secure bool) (*usersmodels
}

// if user not found, return error
if found.ID == "" {
if found.Id == "" {
return nil, &apimodels.Error{
Status: http.StatusNotFound,
Error: apierror.UserNotFound,
Expand Down
Loading

0 comments on commit ee3ca29

Please sign in to comment.