Skip to content

Commit

Permalink
feat: remove origins
Browse files Browse the repository at this point in the history
  • Loading branch information
gllm-dev committed May 23, 2024
1 parent 1d0f4c2 commit e7eb1b7
Show file tree
Hide file tree
Showing 16 changed files with 0 additions and 459 deletions.
41 changes: 0 additions & 41 deletions internal/applications/projectapp/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -263,47 +263,6 @@ func (a *ProjectApplication) RemoveProvider(ctx context.Context, providerID stri
return nil
}

func (a *ProjectApplication) AddAllowedOrigin(ctx context.Context, origin string) error {
a.logger.InfoContext(ctx, "adding allowed origin")
projectID := contexter.GetProjectID(ctx)

err := a.projectRepo.AddAllowedOrigin(ctx, projectID, origin)
if err != nil {
a.logger.ErrorContext(ctx, "failed to add allowed origin", logger.Error(err))
return fromDomainError(err)
}

return nil
}

func (a *ProjectApplication) RemoveAllowedOrigin(ctx context.Context, origin string) error {
a.logger.InfoContext(ctx, "removing allowed origin")

projectID := contexter.GetProjectID(ctx)

err := a.projectRepo.RemoveAllowedOrigin(ctx, projectID, origin)
if err != nil {
a.logger.ErrorContext(ctx, "failed to remove allowed origin", logger.Error(err))
return fromDomainError(err)
}

return nil
}

func (a *ProjectApplication) GetAllowedOrigins(ctx context.Context) ([]string, error) {
a.logger.InfoContext(ctx, "getting allowed origins")

projectID := contexter.GetProjectID(ctx)

origins, err := a.projectRepo.GetAllowedOrigins(ctx, projectID)
if err != nil {
a.logger.ErrorContext(ctx, "failed to get allowed origins", logger.Error(err))
return nil, fromDomainError(err)
}

return origins, nil
}

func (a *ProjectApplication) EncryptProjectShares(ctx context.Context, externalPart string) error {
a.logger.InfoContext(ctx, "encrypting project shares")
projectID := contexter.GetProjectID(ctx)
Expand Down
157 changes: 0 additions & 157 deletions internal/applications/projectapp/app_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -811,163 +811,6 @@ func TestProjectApplication_RemoveProvider(t *testing.T) {
}
}

func TestProjectApplication_AddAllowedOrigin(t *testing.T) {
ctx := contexter.WithProjectID(context.Background(), "project_id")
ctx = contexter.WithUserID(ctx, "user_id")
shareRepo := new(sharemockrepo.MockShareRepository)
projectRepo := new(projectmockrepo.MockProjectRepository)
providerRepo := new(providermockrepo.MockProviderRepository)
projectService := projectsvc.New(projectRepo)
providerService := providersvc.New(providerRepo)
app := New(projectService, projectRepo, providerService, providerRepo, shareRepo)

tc := []struct {
name string
origin string
wantErr error
mock func()
}{
{
name: "success",
origin: "some-origin",
wantErr: nil,
mock: func() {
projectRepo.ExpectedCalls = nil
projectRepo.On("AddAllowedOrigin", mock.Anything, "project_id", "some-origin").Return(nil)
},
},
{
name: "repository error",
origin: "some-origin",
wantErr: ErrInternal,
mock: func() {
projectRepo.ExpectedCalls = nil
projectRepo.On("AddAllowedOrigin", mock.Anything, "project_id", "some-origin").Return(errors.New("repository error"))
},
},
}

for _, tt := range tc {
t.Run(tt.name, func(t *testing.T) {
tt.mock()
ass := assert.New(t)
err := app.AddAllowedOrigin(ctx, tt.origin)
ass.Equal(tt.wantErr, err)
})
}
}

func TestProjectApplication_RemoveAllowedOrigin(t *testing.T) {
ctx := contexter.WithProjectID(context.Background(), "project_id")
ctx = contexter.WithUserID(ctx, "user_id")
shareRepo := new(sharemockrepo.MockShareRepository)
projectRepo := new(projectmockrepo.MockProjectRepository)
providerRepo := new(providermockrepo.MockProviderRepository)
projectService := projectsvc.New(projectRepo)
providerService := providersvc.New(providerRepo)
app := New(projectService, projectRepo, providerService, providerRepo, shareRepo)

tc := []struct {
name string
origin string
wantErr error
mock func()
}{
{
name: "success",
origin: "some-origin",
wantErr: nil,
mock: func() {
projectRepo.ExpectedCalls = nil
projectRepo.On("RemoveAllowedOrigin", mock.Anything, "project_id", "some-origin").Return(nil)
},
},
{
name: "allowed origin not found",
origin: "some-origin",
wantErr: ErrAllowedOriginNotFound,
mock: func() {
projectRepo.ExpectedCalls = nil
projectRepo.On("RemoveAllowedOrigin", mock.Anything, "project_id", "some-origin").Return(domain.ErrAllowedOriginNotFound)
},
},
{
name: "repository error",
origin: "some-origin",
wantErr: ErrInternal,
mock: func() {
projectRepo.ExpectedCalls = nil
projectRepo.On("RemoveAllowedOrigin", mock.Anything, "project_id", "some-origin").Return(errors.New("repository error"))
},
},
}

for _, tt := range tc {
t.Run(tt.name, func(t *testing.T) {
tt.mock()
ass := assert.New(t)
err := app.RemoveAllowedOrigin(ctx, tt.origin)
ass.Equal(tt.wantErr, err)
})
}
}

func TestProjectApplication_GetAllowedOrigins(t *testing.T) {
ctx := contexter.WithProjectID(context.Background(), "project_id")
ctx = contexter.WithUserID(ctx, "user_id")
shareRepo := new(sharemockrepo.MockShareRepository)
projectRepo := new(projectmockrepo.MockProjectRepository)
providerRepo := new(providermockrepo.MockProviderRepository)
projectService := projectsvc.New(projectRepo)
providerService := providersvc.New(providerRepo)
app := New(projectService, projectRepo, providerService, providerRepo, shareRepo)

tc := []struct {
name string
wantOrigins []string
wantErr error
mock func()
}{
{
name: "success",
wantOrigins: []string{"some-origin"},
wantErr: nil,
mock: func() {
projectRepo.ExpectedCalls = nil
projectRepo.On("GetAllowedOrigins", mock.Anything, "project_id").Return([]string{"some-origin"}, nil)
},
},
{
name: "success no origins",
wantOrigins: []string{},
wantErr: nil,
mock: func() {
projectRepo.ExpectedCalls = nil
projectRepo.On("GetAllowedOrigins", mock.Anything, "project_id").Return([]string{}, nil)
},
},
{
name: "error getting allowed origins",
wantOrigins: nil,
wantErr: ErrInternal,
mock: func() {
projectRepo.ExpectedCalls = nil
projectRepo.On("GetAllowedOrigins", mock.Anything, "project_id").Return(nil, errors.New("repository error"))
},
},
}

for _, tt := range tc {
t.Run(tt.name, func(t *testing.T) {
tt.mock()
ass := assert.New(t)
origins, err := app.GetAllowedOrigins(ctx)
ass.Equal(tt.wantErr, err)
ass.Equal(tt.wantOrigins, origins)
})
}
}

func TestProjectApplication_EncryptProjectShares(t *testing.T) {
ctx := contexter.WithProjectID(context.Background(), "project_id")
ctx = contexter.WithUserID(ctx, "user_id")
Expand Down
5 changes: 0 additions & 5 deletions internal/applications/projectapp/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ var (
ErrProviderNotFound = errors.New("custom authentication not found")
ErrInvalidEncryptionPart = errors.New("invalid encryption part")
ErrEncryptionPartAlreadyExists = errors.New("encryption part already exists")
ErrAllowedOriginNotFound = errors.New("allowed origin not found")
ErrEncryptionNotConfigured = errors.New("encryption not configured")
ErrJWKPemConflict = errors.New("jwk and pem cannot be set at the same time")
ErrInternal = errors.New("internal error")
Expand Down Expand Up @@ -47,10 +46,6 @@ func fromDomainError(err error) error {
return ErrProviderNotFound
}

if errors.Is(err, domain.ErrAllowedOriginNotFound) {
return ErrAllowedOriginNotFound
}

if errors.Is(err, domain.ErrEncryptionPartNotFound) {
return ErrEncryptionNotConfigured
}
Expand Down
1 change: 0 additions & 1 deletion internal/core/domain/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ var (
ErrEncryptionPartNotFound = errors.New("encryption part not found")
ErrEncryptionPartAlreadyExists = errors.New("encryption part already exists")
ErrEncryptionPartRequired = errors.New("encryption part is required")
ErrAllowedOriginNotFound = errors.New("allowed origin not found")

// Provider errors
ErrInvalidProviderConfig = errors.New("invalid provider config")
Expand Down
5 changes: 0 additions & 5 deletions internal/core/ports/repositories/project.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,6 @@ type ProjectRepository interface {
GetByAPIKey(ctx context.Context, apiKey string) (*project.Project, error)
Delete(ctx context.Context, projectID string) error

AddAllowedOrigin(ctx context.Context, projectID, origin string) error
RemoveAllowedOrigin(ctx context.Context, projectID, origin string) error
GetAllowedOrigins(ctx context.Context, projectID string) ([]string, error)
GetAllowedOriginsByAPIKey(ctx context.Context, apiKey string) ([]string, error)

GetEncryptionPart(ctx context.Context, projectID string) (string, error)
SetEncryptionPart(ctx context.Context, projectID, part string) error
}
24 changes: 0 additions & 24 deletions internal/infrastructure/authenticationmgr/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,27 +87,3 @@ func (m *Manager) PreRegisterUser(ctx context.Context, userID string, providerTy

return usr.ID, nil
}

func (m *Manager) IsAllowedOrigin(ctx context.Context, apiKey string, origin string) (bool, error) {
if cachedOrigins, cached := m.mapOrigins[apiKey]; cached {
for _, o := range cachedOrigins {
if o == origin {
return true, nil
}
}
}

dbOrigins, err := m.repo.GetAllowedOriginsByAPIKey(ctx, apiKey)
if err != nil {
return false, err
}
m.mapOrigins[apiKey] = dbOrigins

for _, o := range dbOrigins {
if o == origin {
return true, nil
}
}

return false, nil
}
1 change: 0 additions & 1 deletion internal/infrastructure/handlers/rest/api/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@ var (
ErrJWKPemConflict = &Error{"JWK and PEM cannot be set at the same time", "PV_CFG_INVALID", http.StatusConflict}
ErrInvalidEncryptionPart = &Error{"Invalid encryption part", "EC_INVALID", http.StatusBadRequest}
ErrEncryptionPartAlreadyExists = &Error{"Encryption part already exists", "EC_EXISTS", http.StatusConflict}
ErrAllowedOriginNotFound = &Error{"Allowed origin not found", "AO_NOT_FOUND", http.StatusNotFound}

ErrMissingAPIKey = &Error{"Missing API key", "A_MISSING", http.StatusUnauthorized}
ErrMissingAPISecret = &Error{"Missing API secret", "A_MISSING", http.StatusUnauthorized}
Expand Down
2 changes: 0 additions & 2 deletions internal/infrastructure/handlers/rest/projecthdl/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,6 @@ func fromApplicationError(err error) *api.Error {
return api.ErrInvalidEncryptionPart
case errors.Is(err, projectapp.ErrEncryptionPartAlreadyExists):
return api.ErrEncryptionPartAlreadyExists
case errors.Is(err, projectapp.ErrAllowedOriginNotFound):
return api.ErrAllowedOriginNotFound
case errors.Is(err, projectapp.ErrEncryptionNotConfigured):
return api.ErrEncryptionNotConfigured
case errors.Is(err, projectapp.ErrJWKPemConflict):
Expand Down
Loading

0 comments on commit e7eb1b7

Please sign in to comment.