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
66 changes: 19 additions & 47 deletions src/controller/account_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,52 +2,17 @@ package controller

import (
"bytes"
"context"
"encoding/json"
"io"
"log"
"net/http"
"net/http/httptest"
"os"
"testing"

"github.com/stretchr/testify/assert"
"github.com/switcherapi/switcher-gitops/src/config"
"github.com/switcherapi/switcher-gitops/src/db"
"github.com/switcherapi/switcher-gitops/src/model"
"github.com/switcherapi/switcher-gitops/src/repository"
"go.mongodb.org/mongo-driver/mongo"
)

var mongoDb *mongo.Database

func TestMain(m *testing.M) {
setup()
code := m.Run()
shutdown()
os.Exit(code)
}

func setup() {
os.Setenv("GO_ENV", "test")
config.InitEnv()
mongoDb = db.InitDb()
}

func shutdown() {
// Drop the database after all tests have run
mongoDb.Drop(context.Background())
mongoDb.Client().Disconnect(context.Background())
}

func TestCreateAccountHandler(t *testing.T) {
controller := AccountController{
AccountRepository: &repository.AccountRepositoryMongo{Db: mongoDb},
}

w := httptest.NewRecorder()
r := httptest.NewRequest("POST", "/api/account", nil)

// Create a sample account request
accountRequest := model.Account{
Repository: "switcherapi/switcher-gitops",
Expand All @@ -67,22 +32,29 @@ func TestCreateAccountHandler(t *testing.T) {
},
}

// Encode the account request as JSON
body, _ := json.Marshal(accountRequest)
r.Body = io.NopCloser(bytes.NewReader(body))
r.Header.Set("Content-Type", "application/json")

controller.CreateAccountHandler(w, r)
// Create a request and response recorder
w, r := givenAccountRequest(accountRequest)

// Assert the response status code
assert.Equal(t, http.StatusCreated, w.Code)
// Test
accountController.CreateAccountHandler(w, r)

// Assert the response body
// Assert
var accountResponse model.Account
err := json.NewDecoder(w.Body).Decode(&accountResponse)
if err != nil {
log.Fatal(err)
}

assert.Equal(t, http.StatusCreated, w.Code)
assert.Nil(t, err)
assert.Equal(t, accountRequest.Repository, accountResponse.Repository)
}

func givenAccountRequest(data model.Account) (*httptest.ResponseRecorder, *http.Request) {
w := httptest.NewRecorder()
r := httptest.NewRequest("POST", "/api/account", nil)

// Encode the account request as JSON
body, _ := json.Marshal(data)
r.Body = io.NopCloser(bytes.NewReader(body))
r.Header.Set("Content-Type", "application/json")

return w, r
}
14 changes: 9 additions & 5 deletions src/controller/api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,16 @@ import (
)

func TestCheckApiHandler(t *testing.T) {
controller := ApiController{}
w := httptest.NewRecorder()
r := httptest.NewRequest("GET", "/api/check", nil)

controller.CheckApiHandler(w, r)
w, r := givenApiRequest()
apiController.CheckApiHandler(w, r)

assert.Equal(t, http.StatusOK, w.Code)
assert.Equal(t, `{"message":"API is working"}`, w.Body.String())
}

func givenApiRequest() (*httptest.ResponseRecorder, *http.Request) {
w := httptest.NewRecorder()
r := httptest.NewRequest("GET", "/api/check", nil)

return w, r
}
40 changes: 40 additions & 0 deletions src/controller/controller_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package controller

import (
"context"
"os"
"testing"

"github.com/switcherapi/switcher-gitops/src/config"
"github.com/switcherapi/switcher-gitops/src/db"
"github.com/switcherapi/switcher-gitops/src/repository"
"go.mongodb.org/mongo-driver/mongo"
)

var mongoDb *mongo.Database
var accountController AccountController
var apiController ApiController

func TestMain(m *testing.M) {
setup()
code := m.Run()
shutdown()
os.Exit(code)
}

func setup() {
os.Setenv("GO_ENV", "test")
config.InitEnv()
mongoDb = db.InitDb()

apiController = ApiController{}
accountController = AccountController{
AccountRepository: &repository.AccountRepositoryMongo{Db: mongoDb},
}
}

func shutdown() {
// Drop the database after all tests have run
mongoDb.Drop(context.Background())
mongoDb.Client().Disconnect(context.Background())
}