Skip to content

Commit

Permalink
reduce code duplication in tests
Browse files Browse the repository at this point in the history
  • Loading branch information
majewsky committed Jun 18, 2019
1 parent 8440260 commit 99fd72f
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 152 deletions.
80 changes: 5 additions & 75 deletions internal/api/assets_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,44 +31,8 @@ import (
func TestGetAssets(baseT *testing.T) {
t := test.T{T: baseT}
_, hh, validator, _, _ := setupTest(t)

//endpoint requires a token with project access
validator.Forbid("project:access")
assert.HTTPRequest{
Method: "GET",
Path: "/v1/projects/project1/assets/foo",
ExpectStatus: http.StatusForbidden,
}.Check(t.T, hh)
validator.Allow("project:access")

//expect error for unknown project or resource
assert.HTTPRequest{
Method: "GET",
Path: "/v1/projects/project2/assets/foo",
ExpectStatus: http.StatusNotFound,
}.Check(t.T, hh)
assert.HTTPRequest{
Method: "GET",
Path: "/v1/projects/project1/assets/doesnotexist",
ExpectStatus: http.StatusNotFound,
}.Check(t.T, hh)

//the "unknown" resource exists, but it should be 404 regardless because we
//don't have an asset manager for it
assert.HTTPRequest{
Method: "GET",
Path: "/v1/projects/project1/assets/unknown",
ExpectStatus: http.StatusNotFound,
}.Check(t.T, hh)

//expect error for inaccessible resource
validator.Forbid("project:show:foo")
assert.HTTPRequest{
Method: "GET",
Path: "/v1/projects/project1/assets/foo",
ExpectStatus: http.StatusForbidden,
}.Check(t.T, hh)
validator.Allow("project:show:foo")
testCommonEndpointBehavior(t, hh, validator,
"/v1/projects/%s/assets/%s")

//happy path
validator.Forbid("project:edit:foo") //this should not be an issue
Expand All @@ -88,50 +52,16 @@ func TestGetAssets(baseT *testing.T) {
func TestGetAsset(baseT *testing.T) {
t := test.T{T: baseT}
h, hh, validator, _, _ := setupTest(t)
testCommonEndpointBehavior(t, hh, validator,
"/v1/projects/%s/assets/%s/fooasset1")

//endpoint requires a token with project access
validator.Forbid("project:access")
assert.HTTPRequest{
Method: "GET",
Path: "/v1/projects/project1/assets/foo/fooasset1",
ExpectStatus: http.StatusForbidden,
}.Check(t.T, hh)
validator.Allow("project:access")

//expect error for unknown project, resource or asset
assert.HTTPRequest{
Method: "GET",
Path: "/v1/projects/project2/assets/foo/fooasset1",
ExpectStatus: http.StatusNotFound,
}.Check(t.T, hh)
assert.HTTPRequest{
Method: "GET",
Path: "/v1/projects/project1/assets/doesnotexist/fooasset1",
ExpectStatus: http.StatusNotFound,
}.Check(t.T, hh)
//expect error for unknown asset
assert.HTTPRequest{
Method: "GET",
Path: "/v1/projects/project1/assets/foo/doesnotexist",
ExpectStatus: http.StatusNotFound,
}.Check(t.T, hh)

//the "unknown" resource exists, but it should be 404 regardless because we
//don't have an asset manager for it
assert.HTTPRequest{
Method: "GET",
Path: "/v1/projects/project1/assets/unknown/bogusasset",
ExpectStatus: http.StatusNotFound,
}.Check(t.T, hh)

//expect error for inaccessible resource
validator.Forbid("project:show:foo")
assert.HTTPRequest{
Method: "GET",
Path: "/v1/projects/project1/assets/foo/fooasset1",
ExpectStatus: http.StatusForbidden,
}.Check(t.T, hh)
validator.Allow("project:show:foo")

//happy path: just an asset without any operations
validator.Forbid("project:edit:foo") //this should not be an issue
response := assert.JSONObject{
Expand Down
83 changes: 6 additions & 77 deletions internal/api/operations_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,46 +31,10 @@ import (
func TestGetPendingOperationsForResource(baseT *testing.T) {
t := test.T{T: baseT}
h, hh, validator, _, _ := setupTest(t)
testCommonEndpointBehavior(t, hh, validator,
"/v1/projects/%s/resources/%s/operations/pending")

//endpoint requires a token with project access
validator.Forbid("project:access")
assert.HTTPRequest{
Method: "GET",
Path: "/v1/projects/project1/resources/foo/operations/pending",
ExpectStatus: http.StatusForbidden,
}.Check(t.T, hh)
validator.Allow("project:access")

//expect error for unknown project or resource
assert.HTTPRequest{
Method: "GET",
Path: "/v1/projects/project2/resources/foo/operations/pending",
ExpectStatus: http.StatusNotFound,
}.Check(t.T, hh)
assert.HTTPRequest{
Method: "GET",
Path: "/v1/projects/project1/resources/doesnotexist/operations/pending",
ExpectStatus: http.StatusNotFound,
}.Check(t.T, hh)

//the "unknown" resource exists, but it should be 404 regardless because we
//don't have an asset manager for it
assert.HTTPRequest{
Method: "GET",
Path: "/v1/projects/project1/resources/unknown/operations/pending",
ExpectStatus: http.StatusNotFound,
}.Check(t.T, hh)

//expect error for inaccessible resource
validator.Forbid("project:show:foo")
assert.HTTPRequest{
Method: "GET",
Path: "/v1/projects/project1/resources/foo/operations/pending",
ExpectStatus: http.StatusForbidden,
}.Check(t.T, hh)
validator.Allow("project:show:foo")

//happy path: no pending operations
//start-data.sql contains no pending operations
validator.Forbid("project:edit:foo") //this should not be an issue
response := []assert.JSONObject{}
req := assert.HTTPRequest{
Expand Down Expand Up @@ -130,49 +94,14 @@ func TestGetPendingOperationsForResource(baseT *testing.T) {
func TestGetRecentlyFailedOperationsForResource(baseT *testing.T) {
t := test.T{T: baseT}
h, hh, validator, _, _ := setupTest(t)

//endpoint requires a token with project access
validator.Forbid("project:access")
assert.HTTPRequest{
Method: "GET",
Path: "/v1/projects/project1/resources/foo/operations/recently-failed",
ExpectStatus: http.StatusForbidden,
}.Check(t.T, hh)
validator.Allow("project:access")

//expect error for unknown project or resource
assert.HTTPRequest{
Method: "GET",
Path: "/v1/projects/project2/resources/foo/operations/recently-failed",
ExpectStatus: http.StatusNotFound,
}.Check(t.T, hh)
assert.HTTPRequest{
Method: "GET",
Path: "/v1/projects/project1/resources/doesnotexist/operations/recently-failed",
ExpectStatus: http.StatusNotFound,
}.Check(t.T, hh)

//the "unknown" resource exists, but it should be 404 regardless because we
//don't have an asset manager for it
assert.HTTPRequest{
Method: "GET",
Path: "/v1/projects/project1/resources/unknown/operations/recently-failed",
ExpectStatus: http.StatusNotFound,
}.Check(t.T, hh)

//expect error for inaccessible resource
validator.Forbid("project:show:foo")
assert.HTTPRequest{
Method: "GET",
Path: "/v1/projects/project1/resources/foo/operations/recently-failed",
ExpectStatus: http.StatusForbidden,
}.Check(t.T, hh)
validator.Allow("project:show:foo")
testCommonEndpointBehavior(t, hh, validator,
"/v1/projects/%s/resources/%s/operations/recently-failed")

//start-data.sql has a recently failed critical operation for fooasset1, but
//it will not be shown because fooasset1 does not have critical usage levels
//anymore
expectedOps := []assert.JSONObject{}
validator.Forbid("project:edit:foo") //this should not be an issue
assert.HTTPRequest{
Method: "GET",
Path: "/v1/projects/project1/resources/foo/operations/recently-failed",
Expand Down
46 changes: 46 additions & 0 deletions internal/api/shared_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,15 @@
package api

import (
"fmt"
"net/http"

policy "github.com/databus23/goslo.policy"
"github.com/sapcc/castellum/internal/core"
"github.com/sapcc/castellum/internal/db"
"github.com/sapcc/castellum/internal/plugins"
"github.com/sapcc/castellum/internal/test"
"github.com/sapcc/go-bits/assert"
"github.com/sapcc/go-bits/gopherpolicy"
)

Expand Down Expand Up @@ -84,3 +86,47 @@ func (mv *MockValidator) Enforce(rule string, ctx policy.Context) bool {
func p2uint64(x uint64) *uint64 {
return &x
}

func testCommonEndpointBehavior(t test.T, hh http.Handler, validator *MockValidator, pathPattern string) {
path := func(projectID, resourceID string) string {
return fmt.Sprintf(pathPattern, projectID, resourceID)
}

//endpoint requires a token with project access
validator.Forbid("project:access")
assert.HTTPRequest{
Method: "GET",
Path: path("project1", "foo"),
ExpectStatus: http.StatusForbidden,
}.Check(t.T, hh)
validator.Allow("project:access")

//expect error for unknown project or resource
assert.HTTPRequest{
Method: "GET",
Path: path("project2", "foo"),
ExpectStatus: http.StatusNotFound,
}.Check(t.T, hh)
assert.HTTPRequest{
Method: "GET",
Path: path("project1", "doesnotexist"),
ExpectStatus: http.StatusNotFound,
}.Check(t.T, hh)

//the "unknown" resource exists, but it should be 404 regardless because we
//don't have an asset manager for it
assert.HTTPRequest{
Method: "GET",
Path: path("project1", "unknown"),
ExpectStatus: http.StatusNotFound,
}.Check(t.T, hh)

//expect error for inaccessible resource
validator.Forbid("project:show:foo")
assert.HTTPRequest{
Method: "GET",
Path: path("project1", "foo"),
ExpectStatus: http.StatusForbidden,
}.Check(t.T, hh)
validator.Allow("project:show:foo")
}

0 comments on commit 99fd72f

Please sign in to comment.