Skip to content
This repository has been archived by the owner on Jun 26, 2021. It is now read-only.

Commit

Permalink
Merge pull request #13 from factly/test/organisation
Browse files Browse the repository at this point in the history
Test/organisation
  • Loading branch information
dmonark authored Aug 10, 2020
2 parents 33d992a + 3764b13 commit 4761d6b
Show file tree
Hide file tree
Showing 28 changed files with 1,156 additions and 23 deletions.
59 changes: 59 additions & 0 deletions action/organisation/create_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package organisation

import (
"bytes"
"fmt"
"net/http"
"net/http/httptest"
"testing"

"github.com/factly/kavach-server/model"
"github.com/factly/kavach-server/util/test"
"github.com/factly/x/loggerx"
"github.com/go-chi/chi"
)

func TestCreateOrganisation(t *testing.T) {
r := chi.NewRouter()
r.Use(loggerx.Init())
r.Post("/organisations", create)

ts := httptest.NewServer(r)
defer ts.Close()

user := model.User{
Email: "joe@bbc.in",
}

model.DB.Create(&user)

t.Run("organisation title required", func(t *testing.T) {
_, statusCode := test.Request(t, ts, "POST", "/organisations", nil, "1")

if statusCode != http.StatusUnprocessableEntity {
t.Errorf("handler returned wrong status code: got %v want %v",
statusCode, http.StatusUnprocessableEntity)
}
})

t.Run("create organisation", func(t *testing.T) {
resp, statusCode := test.Request(t, ts, "POST", "/organisations", bytes.NewBuffer([]byte(`
{
"title": "BBC"
}
`)), fmt.Sprint(user.Base.ID))

respBody := (resp).(map[string]interface{})

if statusCode != http.StatusCreated {
t.Errorf("handler returned wrong status code: got %v want %v",
statusCode, http.StatusCreated)
}

if respBody["title"] != "BBC" {
t.Errorf("handler returned wrong title: got %v want %v", respBody["title"], "BBC")
}

})

}
1 change: 1 addition & 0 deletions action/organisation/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ func delete(w http.ResponseWriter, r *http.Request) {

// check record exists or not
err = model.DB.First(&organisation).Error

if err != nil {
loggerx.Error(err)
errorx.Render(w, errorx.Parser(errorx.RecordNotFound()))
Expand Down
91 changes: 91 additions & 0 deletions action/organisation/delete_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
package organisation

import (
"fmt"
"net/http"
"net/http/httptest"
"testing"

"github.com/factly/kavach-server/model"
"github.com/factly/kavach-server/util/test"
"github.com/factly/x/loggerx"
"github.com/go-chi/chi"
)

func TestDeleteOrganisation(t *testing.T) {
r := chi.NewRouter()
r.Use(loggerx.Init())
r.Delete("/organisations/{organisation_id}", delete)

ts := httptest.NewServer(r)
defer ts.Close()

user := model.User{
Email: "joe@cnn.in",
}

testUser := model.User{
Email: "editor@bbc.in",
}

org := &model.Organisation{
Title: "CNN",
}

model.DB.Create(&user)
model.DB.Create(&testUser)
model.DB.Model(&model.Organisation{}).Create(&org)
model.DB.Create(&model.OrganisationUser{
OrganisationID: org.ID,
Role: "owner",
UserID: user.Base.ID,
})
model.DB.Create(&model.OrganisationUser{
OrganisationID: org.ID,
Role: "editor",
UserID: testUser.Base.ID,
})

t.Run("invalid organisation id", func(t *testing.T) {
_, statusCode := test.Request(t, ts, "DELETE", "/organisations/invalid_id", nil, "1")

if statusCode != http.StatusNotFound {
t.Errorf("handler returned wrong status code: got %v want %v", statusCode, http.StatusNotFound)
}
})

t.Run("record not found", func(t *testing.T) {
_, statusCode := test.Request(t, ts, "DELETE", "/organisations/100", nil, "1")

if statusCode != http.StatusNotFound {
t.Errorf("handler returned wrong status code: got %v want %v", statusCode, http.StatusNotFound)
}
})

t.Run("invalid user ", func(t *testing.T) {
_, statusCode := test.Request(t, ts, "DELETE", fmt.Sprint("/organisations/", org.Base.ID), nil, "invalid_id")

if statusCode != http.StatusInternalServerError {
t.Errorf("handler returned wrong status code: got %v want %v", statusCode, http.StatusInternalServerError)
}
})

t.Run("user without role owner", func(t *testing.T) {

_, statusCode := test.Request(t, ts, "DELETE", fmt.Sprint("/organisations/", org.Base.ID), nil, fmt.Sprint(testUser.Base.ID))

if statusCode != http.StatusUnprocessableEntity {
t.Errorf("handler returned wrong status code: got %v want %v", statusCode, http.StatusUnprocessableEntity)
}
})

t.Run("delete organisation by id", func(t *testing.T) {

_, statusCode := test.Request(t, ts, "DELETE", fmt.Sprint("/organisations/", org.Base.ID), nil, fmt.Sprint(user.Base.ID))

if statusCode != http.StatusOK {
t.Errorf("handler returned wrong status code: got %v want %v", statusCode, http.StatusOK)
}
})

}
56 changes: 56 additions & 0 deletions action/organisation/details_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package organisation

import (
"fmt"
"net/http"
"net/http/httptest"
"testing"

"github.com/factly/kavach-server/model"
"github.com/factly/kavach-server/util/test"
"github.com/go-chi/chi"
)

func TestOrganisationDetail(t *testing.T) {
r := chi.NewRouter()
r.Get("/organisations/{organisation_id}", details)

ts := httptest.NewServer(r)
defer ts.Close()

t.Run("invalid organisation id", func(t *testing.T) {
_, statusCode := test.Request(t, ts, "GET", "/organisations/test", nil, "1")

if statusCode != http.StatusNotFound {
t.Errorf("handler returned wrong status code: got %v want %v", statusCode, http.StatusNotFound)
}
})

t.Run("record not found", func(t *testing.T) {
_, statusCode := test.Request(t, ts, "GET", "/organisations/100", nil, "1")

if statusCode != http.StatusNotFound {
t.Errorf("handler returned wrong status code: got %v want %v", statusCode, http.StatusNotFound)
}
})

t.Run("get organisation by id", func(t *testing.T) {
org := &model.Organisation{
Title: "TOI",
}

model.DB.Model(&model.Organisation{}).Create(&org)
resp, statusCode := test.Request(t, ts, "GET", fmt.Sprint("/organisations/", org.Base.ID), nil, "1")
respBody := (resp).(map[string]interface{})

if statusCode != http.StatusOK {
t.Errorf("handler returned wrong status code: got %v want %v", statusCode, http.StatusOK)
}

if respBody["title"] != "TOI" {
t.Errorf("handler returned wrong title: got %v want %v", respBody["title"], "TOI")
}

})

}
86 changes: 86 additions & 0 deletions action/organisation/list_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
package organisation

import (
"fmt"
"net/http"
"net/http/httptest"
"testing"

"github.com/factly/kavach-server/model"
"github.com/factly/kavach-server/util/test"
"github.com/factly/x/loggerx"
"github.com/go-chi/chi"
)

func TestListOrganisation(t *testing.T) {
r := chi.NewRouter()
r.Use(loggerx.Init())
r.Get("/organisations/my", list)

ts := httptest.NewServer(r)
defer ts.Close()

organisationUsers := []model.OrganisationUser{}

user := model.User{
Email: "tester@test.in",
}

organisationOne := model.Organisation{
Title: "Tester",
}
organisationTwo := model.Organisation{
Title: "Tester",
}

model.DB.Create(&user)

model.DB.Create(&organisationOne)
model.DB.Create(&organisationTwo)

userOrgOne := model.OrganisationUser{
UserID: user.Base.ID,
OrganisationID: organisationOne.Base.ID,
Role: "owner",
}

userOrgTwo := model.OrganisationUser{
UserID: user.Base.ID,
OrganisationID: organisationTwo.Base.ID,
Role: "owner",
}

model.DB.Create(&userOrgOne)
model.DB.Create(&userOrgTwo)

model.DB.Model(&model.OrganisationUser{}).Where(&model.OrganisationUser{
UserID: user.Base.ID,
}).Preload("Organisation").Find(&organisationUsers)

t.Run("Invalid header", func(t *testing.T) {
_, statusCode := test.Request(t, ts, "GET", "/organisations/my", nil, "abc")

if statusCode != http.StatusBadRequest {
t.Errorf("handler returned wrong status code: got %v want %v", statusCode, http.StatusBadRequest)
}

})

t.Run("get all organisation", func(t *testing.T) {
resp, statusCode := test.Request(t, ts, "GET", "/organisations/my", nil, fmt.Sprint(user.Base.ID))

if statusCode != http.StatusOK {
t.Errorf("handler returned wrong status code: got %v want %v",
statusCode, http.StatusOK)
}

respBody := (resp).([]interface{})

if len(respBody) != len(organisationUsers) {
t.Errorf("handler returned wrong total: got %v want %v",
len(respBody), len(organisationUsers))
}

})

}
21 changes: 21 additions & 0 deletions action/organisation/main_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package organisation

import (
"os"
"testing"

"github.com/factly/kavach-server/model"
"github.com/factly/kavach-server/util/test"
)

func TestMain(m *testing.M) {
os.Setenv("DSN", "postgres://postgres:postgres@localhost:5432/kavach-test?sslmode=disable")
os.Setenv("KETO_API", "http://127.0.0.1:4466")
model.SetupDB()

exitValue := m.Run()

test.CleanTables()

os.Exit(exitValue)
}
16 changes: 16 additions & 0 deletions action/organisation/route_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package organisation

import (
"testing"
)

func TestOrganisationRouter(t *testing.T) {
organisationRouter := Router()
got := len(organisationRouter.Routes()[0].Handlers)
expected := 10

if got != expected {
t.Errorf("handler returned wrong pattern: got %v want %v",
got, expected)
}
}
Loading

0 comments on commit 4761d6b

Please sign in to comment.