Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ GitOps Domain Snapshot Orchestrator for Switcher API
<div align="center">

[![Master CI](https://github.com/switcherapi/switcher-gitops/actions/workflows/master.yml/badge.svg?branch=master)](https://github.com/switcherapi/switcher-gitops/actions/workflows/master.yml)
[![Quality Gate Status](https://sonarcloud.io/api/project_badges/measure?project=switcherapi_switcher-gitops&metric=alert_status)](https://sonarcloud.io/dashboard?id=switcherapi_switcher-gitops)
[![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)
[![Slack: Switcher-HQ](https://img.shields.io/badge/slack-@switcher/hq-blue.svg?logo=slack)](https://switcher-hq.slack.com/)

Expand Down
36 changes: 35 additions & 1 deletion src/controller/account_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down Expand Up @@ -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))
Expand Down Expand Up @@ -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)
Expand Down
4 changes: 3 additions & 1 deletion src/controller/controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
22 changes: 22 additions & 0 deletions src/repository/account.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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()
Expand Down Expand Up @@ -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)
}
}
4 changes: 3 additions & 1 deletion src/server/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down