126 lines
4.1 KiB
Go
126 lines
4.1 KiB
Go
package handlers_test
|
|
|
|
import (
|
|
"fmt"
|
|
"net/http"
|
|
"otaupdate/handlers"
|
|
"otaupdate/services"
|
|
"testing"
|
|
|
|
"github.com/fiskerinc/cloud-services/pkg/common"
|
|
orm "github.com/fiskerinc/cloud-services/pkg/db/queries"
|
|
mo "github.com/fiskerinc/cloud-services/pkg/db/queries/mocks"
|
|
th "github.com/fiskerinc/cloud-services/pkg/testhelper"
|
|
"github.com/google/uuid"
|
|
)
|
|
|
|
func TestSubscriptionPackagesGetList(t *testing.T) {
|
|
mock := mo.MockSubscriptionPackages{}
|
|
services.GetDB().SetSubPackages(&mock)
|
|
expectedResp := `{"data":[{"id":"0557bd1d-76d3-41e5-a44e-13c479e55ab0","name":"Test Package"}],"total":1}`
|
|
expectedRespNoTotal := `{"data":[{"id":"0557bd1d-76d3-41e5-a44e-13c479e55ab0","name":"Test Package"}]}`
|
|
defaultOrder := "created_at DESC"
|
|
listData := []common.SubscriptionPackage{
|
|
{
|
|
ID: uuid.MustParse("0557bd1d-76d3-41e5-a44e-13c479e55ab0"),
|
|
Name: "Test Package",
|
|
},
|
|
}
|
|
|
|
tests := []mo.DBHttpTest{
|
|
{
|
|
Name: "No parameters",
|
|
Request: th.MakeTestRequest(http.MethodGet, "http://example.com/subscriptionpackages", nil),
|
|
ExpectedStatus: http.StatusOK,
|
|
ExpectedResponse: expectedResp,
|
|
DBTestCase: mo.DBTestCase{
|
|
ExpectedFilter: &common.SubscriptionPackage{},
|
|
ExpectedPage: &orm.PageQueryOptions{
|
|
Order: defaultOrder,
|
|
Limit: orm.PageQueryOptionsLimitMaximum,
|
|
Offset: 0,
|
|
},
|
|
MockListResponse: listData,
|
|
},
|
|
},
|
|
{
|
|
Name: "Id parameter",
|
|
Request: th.MakeTestRequest(http.MethodGet, "http://example.com/subscriptionpackages?id=0557bd1d-76d3-41e5-a44e-13c479e55ab0", nil),
|
|
ExpectedStatus: http.StatusOK,
|
|
ExpectedResponse: expectedRespNoTotal,
|
|
DBTestCase: mo.DBTestCase{
|
|
ExpectedFilter: &common.SubscriptionPackage{
|
|
ID: uuid.MustParse("0557bd1d-76d3-41e5-a44e-13c479e55ab0"),
|
|
},
|
|
ExpectedPage: &orm.PageQueryOptions{
|
|
Order: defaultOrder,
|
|
Limit: orm.PageQueryOptionsLimitMaximum,
|
|
Offset: 0,
|
|
},
|
|
MockListResponse: listData,
|
|
},
|
|
},
|
|
{
|
|
Name: "Name parameter",
|
|
Request: th.MakeTestRequest(http.MethodGet, "http://example.com/subscriptionpackages?name=Test", nil),
|
|
ExpectedStatus: http.StatusOK,
|
|
ExpectedResponse: expectedRespNoTotal,
|
|
DBTestCase: mo.DBTestCase{
|
|
ExpectedFilter: &common.SubscriptionPackage{
|
|
Name: "Test",
|
|
},
|
|
ExpectedPage: &orm.PageQueryOptions{
|
|
Order: defaultOrder,
|
|
Limit: orm.PageQueryOptionsLimitMaximum,
|
|
Offset: 0,
|
|
},
|
|
MockListResponse: listData,
|
|
},
|
|
},
|
|
{
|
|
Name: "Paging parameters",
|
|
Request: th.MakeTestRequest(http.MethodGet, "http://example.com/subscriptionpackages?offset=10&limit=5", nil),
|
|
ExpectedStatus: http.StatusOK,
|
|
ExpectedResponse: expectedRespNoTotal,
|
|
DBTestCase: mo.DBTestCase{
|
|
ExpectedFilter: &common.SubscriptionPackage{},
|
|
ExpectedPage: &orm.PageQueryOptions{
|
|
Order: defaultOrder,
|
|
Limit: 5,
|
|
Offset: 10,
|
|
},
|
|
MockListResponse: listData,
|
|
},
|
|
},
|
|
{
|
|
Name: "Error",
|
|
Request: th.MakeTestRequest(http.MethodGet, "http://example.com/subscriptionpackages", nil),
|
|
ExpectedStatus: http.StatusServiceUnavailable,
|
|
ExpectedResponse: `{"message":"something went wrong","error":"Service Unavailable"}`,
|
|
DBTestCase: mo.DBTestCase{
|
|
ExpectedFilter: &common.SubscriptionPackage{},
|
|
ExpectedPage: &orm.PageQueryOptions{
|
|
Order: defaultOrder,
|
|
Limit: orm.PageQueryOptionsLimitMaximum,
|
|
Offset: 0,
|
|
},
|
|
MockError: fmt.Errorf("something went wrong"),
|
|
},
|
|
},
|
|
{
|
|
Name: "Wrong limit, -100",
|
|
Request: th.MakeTestRequest(http.MethodGet, "http://example.com/subscriptionpackages?name=Test&limit=-100", nil),
|
|
ExpectedStatus: http.StatusBadRequest,
|
|
ExpectedResponse: `{"message":"Limit less than 0","error":"Bad Request"}`,
|
|
},
|
|
{
|
|
Name: "Wrong limit, 1000",
|
|
Request: th.MakeTestRequest(http.MethodGet, "http://example.com/subscriptionpackages?name=Test&limit=1000", nil),
|
|
ExpectedStatus: http.StatusBadRequest,
|
|
ExpectedResponse: `{"message":"Limit greater than 100","error":"Bad Request"}`,
|
|
},
|
|
}
|
|
|
|
mo.RunDBTests(t, tests, handlers.HandleSubscriptionPackagesGetList, &mock)
|
|
}
|