statping/handlers/groups_test.go

170 lines
4.1 KiB
Go
Raw Normal View History

2020-03-04 10:29:00 +00:00
package handlers
import (
2020-04-19 08:03:50 +00:00
"github.com/statping/statping/types/groups"
2020-03-08 18:13:27 +00:00
"github.com/stretchr/testify/assert"
2020-04-19 08:03:50 +00:00
"github.com/stretchr/testify/require"
2020-03-04 10:29:00 +00:00
"testing"
)
2020-04-19 08:03:50 +00:00
func TestUnAuthenticatedGroupRoutes(t *testing.T) {
tests := []HTTPTest{
{
Name: "No Authentication - New Group",
URL: "/api/groups",
Method: "POST",
ExpectedStatus: 401,
BeforeTest: UnsetTestENV,
},
{
Name: "No Authentication - Update Group",
URL: "/api/groups/1",
Method: "POST",
ExpectedStatus: 401,
BeforeTest: UnsetTestENV,
},
{
Name: "No Authentication - Delete Group",
URL: "/api/groups/1",
Method: "DELETE",
ExpectedStatus: 401,
BeforeTest: UnsetTestENV,
},
}
for _, v := range tests {
t.Run(v.Name, func(t *testing.T) {
str, t, err := RunHTTPTest(v, t)
t.Logf("Test %s: \n %v\n", v.Name, str)
assert.Nil(t, err)
})
}
}
2020-03-04 10:29:00 +00:00
func TestGroupAPIRoutes(t *testing.T) {
tests := []HTTPTest{
{
2020-03-08 01:23:41 +00:00
Name: "Statping Public Groups",
2020-03-04 10:29:00 +00:00
URL: "/api/groups",
Method: "GET",
ExpectedStatus: 200,
ResponseLen: 3,
2020-03-08 01:23:41 +00:00
BeforeTest: SetTestENV,
AfterTest: UnsetTestENV,
2020-03-04 10:29:00 +00:00
},
2020-03-08 01:23:41 +00:00
{
Name: "Statping View Public Group",
2020-03-04 10:29:00 +00:00
URL: "/api/groups/1",
Method: "GET",
ExpectedStatus: 200,
2020-03-08 01:23:41 +00:00
BeforeTest: SetTestENV,
AfterTest: UnsetTestENV,
2020-03-04 10:29:00 +00:00
},
{
2020-03-08 01:23:41 +00:00
Name: "Statping Create Public Group",
2020-03-04 10:29:00 +00:00
URL: "/api/groups",
HttpHeaders: []string{"Content-Type=application/json"},
Body: `{
2020-03-08 01:23:41 +00:00
"name": "New Group",
"public": true
}`,
2020-03-04 10:29:00 +00:00
Method: "POST",
ExpectedStatus: 200,
2020-03-08 01:23:41 +00:00
BeforeTest: SetTestENV,
},
{
Name: "Statping Create Private Group",
URL: "/api/groups",
HttpHeaders: []string{"Content-Type=application/json"},
Body: `{
"name": "New Private Group",
"public": false
}`,
Method: "POST",
ExpectedStatus: 200,
},
2020-04-19 08:03:50 +00:00
{
Name: "Incorrect JSON POST",
URL: "/api/groups",
Body: BadJSON,
ExpectedContains: []string{BadJSONResponse},
Method: "POST",
ExpectedStatus: 422,
},
2020-03-08 21:32:26 +00:00
{
Name: "Statping Public and Private Groups",
URL: "/api/groups",
Method: "GET",
ExpectedStatus: 200,
2020-04-09 02:10:43 +00:00
ResponseLen: 3,
2020-03-08 21:32:26 +00:00
BeforeTest: UnsetTestENV,
},
2020-03-08 01:23:41 +00:00
{
Name: "Statping View Private Group",
URL: "/api/groups/2",
Method: "GET",
2020-03-08 18:13:27 +00:00
ExpectedStatus: 401,
2020-03-08 01:23:41 +00:00
BeforeTest: UnsetTestENV,
},
2020-03-08 18:13:27 +00:00
{
Name: "Statping View Private Group Allowed",
URL: "/api/groups/2",
Method: "GET",
ExpectedStatus: 200,
BeforeTest: SetTestENV,
},
2020-03-19 01:50:53 +00:00
{
Name: "Statping Reorder Groups",
URL: "/api/reorder/groups",
Method: "POST",
Body: `[{"group":1,"order":2},{"group":2,"order":1}]`,
ExpectedStatus: 200,
HttpHeaders: []string{"Content-Type=application/json"},
BeforeTest: SetTestENV,
SecureRoute: true,
},
2020-03-08 01:23:41 +00:00
{
Name: "Statping View Unknown Group",
URL: "/api/groups/8383883838",
Method: "GET",
2020-03-19 01:50:53 +00:00
BeforeTest: SetTestENV,
2020-03-08 01:23:41 +00:00
ExpectedStatus: 404,
2020-03-04 10:29:00 +00:00
},
{
2020-04-19 08:03:50 +00:00
Name: "Statping Update Group",
URL: "/api/groups/1",
Method: "POST",
Body: `{
"name": "Updated Group",
"public": false
}`,
ExpectedStatus: 200,
ExpectedContains: []string{Success, MethodUpdate},
BeforeTest: SetTestENV,
SecureRoute: true,
AfterTest: func(t *testing.T) error {
g, err := groups.Find(1)
require.Nil(t, err)
assert.Equal(t, "Updated Group", g.Name)
return nil
},
},
{
Name: "Statping Delete Group",
URL: "/api/groups/1",
Method: "DELETE",
ExpectedStatus: 200,
ExpectedContains: []string{Success, MethodDelete},
AfterTest: UnsetTestENV,
SecureRoute: true,
},
}
2020-03-04 10:29:00 +00:00
for _, v := range tests {
t.Run(v.Name, func(t *testing.T) {
_, t, err := RunHTTPTest(v, t)
2020-03-08 18:13:27 +00:00
assert.Nil(t, err)
2020-03-04 10:29:00 +00:00
})
}
}