statping/handlers/checkins_test.go

116 lines
2.7 KiB
Go
Raw Normal View History

2020-03-04 10:29:00 +00:00
package handlers
import (
2020-03-08 18:13:27 +00:00
"github.com/stretchr/testify/assert"
2020-03-04 10:29:00 +00:00
"testing"
)
2020-04-19 08:03:50 +00:00
func TestUnAuthenticatedCheckinRoutes(t *testing.T) {
tests := []HTTPTest{
{
Name: "No Authentication - New Checkin",
URL: "/api/checkins",
Method: "POST",
ExpectedStatus: 401,
BeforeTest: UnsetTestENV,
},
{
Name: "No Authentication - Delete Checkin",
URL: "/api/checkins/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 TestApiCheckinRoutes(t *testing.T) {
tests := []HTTPTest{
2020-04-16 17:32:54 +00:00
{
Name: "Statping Create Checkins",
URL: "/api/checkins",
Method: "POST",
ExpectedStatus: 200,
BeforeTest: SetTestENV,
SecureRoute: true,
ExpectedContains: []string{Success},
Body: `{
"name": "Example Checkin",
"service_id": 1,
"interval": 300,
2020-04-19 08:03:50 +00:00
"api_key": "example"
2020-04-16 17:32:54 +00:00
}`,
},
2020-03-04 10:29:00 +00:00
{
Name: "Statping Checkins",
URL: "/api/checkins",
Method: "GET",
ExpectedStatus: 200,
2020-04-16 17:32:54 +00:00
ResponseLen: 3,
2020-03-08 01:23:41 +00:00
BeforeTest: SetTestENV,
2020-04-16 17:32:54 +00:00
},
{
2020-04-19 08:03:50 +00:00
Name: "Statping View Checkin",
URL: "/api/checkins/example",
Method: "GET",
ExpectedStatus: 200,
BeforeTest: SetTestENV,
SecureRoute: true,
2020-04-16 17:32:54 +00:00
},
{
2020-04-19 08:03:50 +00:00
Name: "Statping Trigger Checkin",
URL: "/checkin/example",
Method: "GET",
ExpectedStatus: 200,
SecureRoute: true,
BeforeTest: SetTestENV,
2020-04-16 17:32:54 +00:00
},
2020-04-17 03:21:17 +00:00
{
Name: "Statping Missing Trigger Checkin",
2020-04-19 08:03:50 +00:00
URL: "/checkin/missing123",
2020-04-17 03:21:17 +00:00
Method: "GET",
BeforeTest: SetTestENV,
ExpectedStatus: 404,
},
{
Name: "Statping Missing Checkin",
URL: "/api/checkins/missing123",
Method: "GET",
BeforeTest: SetTestENV,
ExpectedStatus: 404,
},
2020-04-19 08:03:50 +00:00
{
Name: "Statping Delete Checkin",
URL: "/api/checkins/example",
Method: "DELETE",
BeforeTest: SetTestENV,
ExpectedContains: []string{Success},
ExpectedStatus: 200,
},
{
Name: "Incorrect JSON POST",
URL: "/api/checkins",
Body: BadJSON,
ExpectedContains: []string{BadJSONResponse},
Method: "POST",
ExpectedStatus: 422,
},
2020-04-16 17:32:54 +00:00
}
2020-03-04 10:29:00 +00:00
for _, v := range tests {
t.Run(v.Name, func(t *testing.T) {
2020-03-08 01:23:41 +00:00
str, t, err := RunHTTPTest(v, t)
t.Logf("Test %s: \n %v\n", v.Name, str)
2020-03-08 18:13:27 +00:00
assert.Nil(t, err)
2020-03-04 10:29:00 +00:00
})
}
}