diff --git a/README.md b/README.md
index d425ca0..e17a373 100644
--- a/README.md
+++ b/README.md
@@ -8,6 +8,7 @@ GitOps Domain Snapshot Orchestrator for Switcher API
[](https://github.com/switcherapi/switcher-gitops/actions/workflows/master.yml)
+[](https://sonarcloud.io/dashboard?id=switcherapi_switcher-gitops)
[](https://opensource.org/licenses/MIT)
[](https://switcher-hq.slack.com/)
diff --git a/src/controller/account_test.go b/src/controller/account_test.go
index 52e2ac1..848a18c 100644
--- a/src/controller/account_test.go
+++ b/src/controller/account_test.go
@@ -41,6 +41,21 @@ func TestCreateAccountHandlerInvalidRequest(t *testing.T) {
assert.Equal(t, "{\"error\":\"Invalid request\"}", w.Body.String())
}
+func TestCreateAccountHandlerErrorCreatingAccount(t *testing.T) {
+ // Create an account
+ accountController.CreateAccountHandler(givenAccountRequest(accountV1))
+
+ // Create a request and response recorder
+ w, r := givenAccountRequest(accountV1)
+
+ // Test
+ accountController.CreateAccountHandler(w, r)
+
+ // Assert
+ assert.Equal(t, http.StatusInternalServerError, w.Code)
+ assert.Equal(t, "{\"error\":\"Error creating account\"}", w.Body.String())
+}
+
func TestFetchAccountHandlerByDomainId(t *testing.T) {
// Create an account
accountController.CreateAccountHandler(givenAccountRequest(accountV1))
@@ -106,6 +121,25 @@ func TestUpdateAccountHandlerInvalidRequest(t *testing.T) {
assert.Equal(t, "{\"error\":\"Invalid request\"}", w.Body.String())
}
+func TestUpdateAccountHandlerErrorUpdatingAccount(t *testing.T) {
+ // Create an account
+ accountV1Copy := accountV1
+ accountController.CreateAccountHandler(givenAccountRequest(accountV1Copy))
+
+ // Replace the domain ID to force an error
+ accountV1Copy.Domain.ID = "111"
+
+ // Create a request and response recorder
+ w, r := givenAccountRequest(accountV1Copy)
+
+ // Test
+ accountController.UpdateAccountHandler(w, r)
+
+ // Assert
+ assert.Equal(t, http.StatusInternalServerError, w.Code)
+ assert.Equal(t, "{\"error\":\"Error updating account\"}", w.Body.String())
+}
+
func TestDeleteAccountHandler(t *testing.T) {
// Create an account
accountController.CreateAccountHandler(givenAccountRequest(accountV1))
@@ -138,7 +172,7 @@ func TestDeleteAccountHandlerNotFound(t *testing.T) {
func givenAccountRequest(data model.Account) (*httptest.ResponseRecorder, *http.Request) {
w := httptest.NewRecorder()
- r := httptest.NewRequest("POST", accountController.RouteAccountPath, nil)
+ r := httptest.NewRequest(http.MethodPost, accountController.RouteAccountPath, nil)
// Encode the account request as JSON
body, _ := json.Marshal(data)
diff --git a/src/controller/controller_test.go b/src/controller/controller_test.go
index a2eb41c..ce98aa3 100644
--- a/src/controller/controller_test.go
+++ b/src/controller/controller_test.go
@@ -28,8 +28,10 @@ func setup() {
config.InitEnv()
mongoDb = db.InitDb()
+ accountRepository := repository.NewAccountRepositoryMongo(mongoDb)
+
apiController = NewApiController()
- accountController = NewAccountController(&repository.AccountRepositoryMongo{Db: mongoDb})
+ accountController = NewAccountController(accountRepository)
}
func shutdown() {
diff --git a/src/repository/account.go b/src/repository/account.go
index ae3ba68..c4be9e3 100644
--- a/src/repository/account.go
+++ b/src/repository/account.go
@@ -2,11 +2,14 @@ package repository
import (
"context"
+ "log"
"time"
"github.com/switcherapi/switcher-gitops/src/model"
+ "go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/bson/primitive"
"go.mongodb.org/mongo-driver/mongo"
+ "go.mongodb.org/mongo-driver/mongo/options"
)
type AccountRepository interface {
@@ -30,6 +33,11 @@ func (err ErrAccountNotFound) Error() string {
const domainIdFilter = "domain.id"
+func NewAccountRepositoryMongo(db *mongo.Database) *AccountRepositoryMongo {
+ registerAccountRepositoryValidators(db)
+ return &AccountRepositoryMongo{Db: db}
+}
+
func (repo *AccountRepositoryMongo) Create(account *model.Account) (*model.Account, error) {
collection, ctx, cancel := getDbContext(repo)
defer cancel()
@@ -93,3 +101,17 @@ func getDbContext(repo *AccountRepositoryMongo) (*mongo.Collection, context.Cont
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
return collection, ctx, cancel
}
+
+func registerAccountRepositoryValidators(db *mongo.Database) {
+ collection := db.Collection(model.CollectionName)
+ indexOptions := options.Index().SetUnique(true)
+ indexModel := mongo.IndexModel{
+ Keys: bson.M{domainIdFilter: 1},
+ Options: indexOptions,
+ }
+
+ _, err := collection.Indexes().CreateOne(context.Background(), indexModel)
+ if err != nil {
+ log.Fatal(err)
+ }
+}
diff --git a/src/server/app.go b/src/server/app.go
index 238c8fc..45ed4ed 100644
--- a/src/server/app.go
+++ b/src/server/app.go
@@ -58,8 +58,10 @@ func (app *App) Start() error {
}
func initRoutes(db *mongo.Database) *mux.Router {
+ accountRepository := repository.NewAccountRepositoryMongo(db)
+
apiController := controller.NewApiController()
- accountController := controller.NewAccountController(&repository.AccountRepositoryMongo{Db: db})
+ accountController := controller.NewAccountController(accountRepository)
r := mux.NewRouter()
apiController.RegisterRoutes(r)