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 TestSubscriptionFeaturesGetList(t *testing.T) { mock := mo.MockSubscriptionFeatures{} services.GetDB().SetSubFeatures(&mock) testFeatureID := uuid.MustParse("ecfb89e0-ca03-4aa9-a43a-a9d703256edb") expectedResp := `{"data":[{"id":"ecfb89e0-ca03-4aa9-a43a-a9d703256edb","name":"Test Feature","description":"Test Description"}],"total":1}` expectedRespNoTotal := `{"data":[{"id":"ecfb89e0-ca03-4aa9-a43a-a9d703256edb","name":"Test Feature","description":"Test Description"}]}` defaultOrder := "created_at DESC" listData := []common.SubscriptionFeature{ { ID: testFeatureID, Name: "Test Feature", Description: "Test Description", }, } 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.SubscriptionFeature{}, 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.SubscriptionFeature{ 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/subscriptionfeatures?name=Test%20Feature", nil), ExpectedStatus: http.StatusOK, ExpectedResponse: expectedRespNoTotal, DBTestCase: mo.DBTestCase{ ExpectedFilter: &common.SubscriptionFeature{ Name: "Test Feature", }, ExpectedPage: &orm.PageQueryOptions{ Order: defaultOrder, Limit: orm.PageQueryOptionsLimitMaximum, Offset: 0, }, MockListResponse: listData, }, }, { Name: "Paging parameters", Request: th.MakeTestRequest(http.MethodGet, "http://example.com/subscriptionfeatures?offset=10&limit=5", nil), ExpectedStatus: http.StatusOK, ExpectedResponse: expectedRespNoTotal, DBTestCase: mo.DBTestCase{ ExpectedFilter: &common.SubscriptionFeature{}, ExpectedPage: &orm.PageQueryOptions{ Order: defaultOrder, Limit: 5, Offset: 10, }, MockListResponse: listData, }, }, { Name: "Error", Request: th.MakeTestRequest(http.MethodGet, "http://example.com/subscriptionfeatures", nil), ExpectedStatus: http.StatusServiceUnavailable, ExpectedResponse: `{"message":"something went wrong","error":"Service Unavailable"}`, DBTestCase: mo.DBTestCase{ ExpectedFilter: &common.SubscriptionFeature{}, 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?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?limit=1000", nil), ExpectedStatus: http.StatusBadRequest, ExpectedResponse: `{"message":"Limit greater than 100","error":"Bad Request"}`, }, } mo.RunDBTests(t, tests, handlers.HandleSubscriptionFeaturesGet, &mock) }