Skip to content

Commit

Permalink
Hotfix/update workspace and repository (#166)
Browse files Browse the repository at this point in the history
* Fixing error that update was not changing to empty description

Signed-off-by: nathanmartinszup <[email protected]>
  • Loading branch information
nathanmartinszup authored Aug 24, 2021
1 parent d95d4e5 commit 66b0cef
Show file tree
Hide file tree
Showing 9 changed files with 155 additions and 25 deletions.
19 changes: 16 additions & 3 deletions core/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,29 @@ module github.com/ZupIT/horusec-platform/core
go 1.16

require (
github.com/ZupIT/horusec-devkit v1.0.11
github.com/ZupIT/horusec-devkit v1.0.14
github.com/alecthomas/template v0.0.0-20190718012654-fb15b899a751
github.com/cosmtrek/air v1.27.3 // indirect
github.com/cespare/xxhash/v2 v2.1.2 // indirect
github.com/form3tech-oss/jwt-go v3.2.5+incompatible // indirect
github.com/go-chi/chi v4.1.2+incompatible
github.com/go-chi/cors v1.2.0
github.com/go-ozzo/ozzo-validation/v4 v4.3.0
github.com/golang-jwt/jwt v3.2.2+incompatible // indirect
github.com/google/uuid v1.3.0
github.com/google/wire v0.5.0
github.com/jackc/pgx/v4 v4.13.0 // indirect
github.com/lib/pq v1.10.2
github.com/prometheus/common v0.30.0 // indirect
github.com/prometheus/procfs v0.7.3 // indirect
github.com/stretchr/testify v1.7.0
github.com/swaggo/swag v1.7.0
github.com/swaggo/files v0.0.0-20210815190702-a29dd2bc99b2 // indirect
github.com/swaggo/http-swagger v1.1.1 // indirect
github.com/swaggo/swag v1.7.1
golang.org/x/crypto v0.0.0-20210817164053-32db794688a5 // indirect
golang.org/x/net v0.0.0-20210813160813-60bc85c4be6d // indirect
golang.org/x/sys v0.0.0-20210823070655-63515b42dcdf // indirect
golang.org/x/text v0.3.7 // indirect
golang.org/x/tools v0.1.5 // indirect
google.golang.org/genproto v0.0.0-20210821163610-241b8fcbd6c8 // indirect
gorm.io/gorm v1.21.13 // indirect
)
57 changes: 43 additions & 14 deletions core/go.sum

Large diffs are not rendered by default.

8 changes: 4 additions & 4 deletions core/internal/controllers/repository/repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -160,10 +160,10 @@ func (c *Controller) Update(data *repositoryEntities.Data) (*repositoryEntities.
if repository.Name != data.Name && !c.useCases.IsNotFoundError(err) {
return nil, repositoryEnums.ErrorRepositoryNameAlreadyInUse
}
nr := repository.Update(data)
filterToUpdate := c.useCases.FilterRepositoryByID(data.RepositoryID)
err = c.databaseWrite.Update(nr, filterToUpdate, repositoryEnums.DatabaseRepositoryTable).GetError()
return nr.ToRepositoryResponse(accountEnums.Admin), err

return repository.Update(data).ToRepositoryResponse(accountEnums.Admin),
c.databaseWrite.Update(repository.ToUpdateMap(data), c.useCases.FilterRepositoryByID(data.RepositoryID),
repositoryEnums.DatabaseRepositoryTable).GetError()
}

func (c *Controller) Delete(repositoryID uuid.UUID) error {
Expand Down
7 changes: 3 additions & 4 deletions core/internal/controllers/workspace/workspace.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,10 +134,9 @@ func (c *Controller) Update(data *workspaceEntities.Data) (*workspaceEntities.Re
return nil, err
}

nw := workspace.Update(data)
filterToUpdate := c.useCases.FilterWorkspaceByID(data.WorkspaceID)
err = c.databaseWrite.Update(nw, filterToUpdate, workspaceEnums.DatabaseWorkspaceTable).GetError()
return nw.ToWorkspaceResponse(accountEnums.Admin), err
return workspace.Update(data).ToWorkspaceResponse(accountEnums.Admin), c.databaseWrite.Update(
workspace.ToUpdateMap(data), c.useCases.FilterWorkspaceByID(data.WorkspaceID),
workspaceEnums.DatabaseWorkspaceTable).GetError()
}

func (c *Controller) Delete(workspaceID uuid.UUID) error {
Expand Down
1 change: 1 addition & 0 deletions core/internal/entities/repository/account_repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
roleEntities "github.com/ZupIT/horusec-platform/core/internal/entities/role"
)

// TODO add unity tests
type AccountRepository struct {
RepositoryID uuid.UUID `json:"repositoryID"`
AccountID uuid.UUID `json:"accountID"`
Expand Down
11 changes: 11 additions & 0 deletions core/internal/entities/repository/repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,3 +78,14 @@ func (r *Repository) ContainsAllAuthzGroups() bool {

return true
}

func (r *Repository) ToUpdateMap(data *Data) map[string]interface{} {
return map[string]interface{}{
"name": data.Name,
"description": data.Description,
"authz_member": pq.Array(data.AuthzMember),
"authz_supervisor": pq.Array(data.AuthzSupervisor),
"authz_admin": pq.Array(data.AuthzAdmin),
"updated_at": time.Now(),
}
}
37 changes: 37 additions & 0 deletions core/internal/entities/repository/repository_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,3 +141,40 @@ func TestContainsAllAuthzGroups(t *testing.T) {
assert.False(t, repository.ContainsAllAuthzGroups())
})
}

func TestToUpdateMap(t *testing.T) {
t.Run("should success parse to update map", func(t *testing.T) {
repository := &Repository{
RepositoryID: uuid.New(),
WorkspaceID: uuid.New(),
Name: "test1",
Description: "test1",
AuthzMember: []string{"test1"},
AuthzAdmin: []string{"test1"},
AuthzSupervisor: []string{"test1"},
CreatedAt: time.Now(),
UpdatedAt: time.Now(),
}

data := &Data{
AccountID: uuid.Nil,
WorkspaceID: uuid.Nil,
Name: "test2",
Description: "test2",
AuthzMember: []string{"test2"},
AuthzAdmin: []string{"test2"},
AuthzSupervisor: []string{"test2"},
Permissions: nil,
}

assert.NotPanics(t, func() {
result := repository.ToUpdateMap(data)
assert.Equal(t, "test2", result["name"])
assert.Equal(t, "test2", result["description"])
assert.Equal(t, pq.Array([]string{"test2"}), result["authz_member"])
assert.Equal(t, pq.Array([]string{"test2"}), result["authz_supervisor"])
assert.Equal(t, pq.Array([]string{"test2"}), result["authz_admin"])
assert.NotEqual(t, repository.UpdatedAt, result["updated_at"])
})
})
}
10 changes: 10 additions & 0 deletions core/internal/entities/workspace/workspace.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,3 +64,13 @@ func (w *Workspace) Update(data *Data) *Workspace {
w.UpdatedAt = time.Now()
return w
}

func (w *Workspace) ToUpdateMap(data *Data) map[string]interface{} {
return map[string]interface{}{
"name": data.Name,
"description": data.Description,
"authz_member": pq.Array(data.AuthzMember),
"authz_admin": pq.Array(data.AuthzAdmin),
"updated_at": time.Now(),
}
}
30 changes: 30 additions & 0 deletions core/internal/entities/workspace/workspace_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,3 +101,33 @@ func TestUpdateWorkspace(t *testing.T) {
assert.NotEqual(t, expectedTime, workspace.UpdatedAt)
})
}

func TestToUpdateMap(t *testing.T) {
t.Run("should success parse to update map", func(t *testing.T) {
workspace := &Workspace{
WorkspaceID: uuid.New(),
Name: "test1",
Description: "test1",
AuthzMember: []string{"test1"},
AuthzAdmin: []string{"test1"},
CreatedAt: time.Now(),
UpdatedAt: time.Now(),
}

data := &Data{
Name: "test2",
Description: "test2",
AuthzMember: []string{"test2"},
AuthzAdmin: []string{"test2"},
}

assert.NotPanics(t, func() {
result := workspace.ToUpdateMap(data)
assert.Equal(t, "test2", result["name"])
assert.Equal(t, "test2", result["description"])
assert.Equal(t, pq.Array([]string{"test2"}), result["authz_member"])
assert.Equal(t, pq.Array([]string{"test2"}), result["authz_admin"])
assert.NotEqual(t, workspace.UpdatedAt, result["updated_at"])
})
})
}

0 comments on commit 66b0cef

Please sign in to comment.