[XAPID-1037] add more tests and fix test suite
diff --git a/api.go b/api.go index 220da6e..784ffcf 100644 --- a/api.go +++ b/api.go
@@ -91,6 +91,7 @@ // 2. verify params // TODO : make this method of verifyApiKeyReq struct + // TODO : move validation to verifyApiKey struct validate method if verifyApiKeyReq.Action == "" || verifyApiKeyReq.ApiProxyName == "" || verifyApiKeyReq.OrganizationName == "" || verifyApiKeyReq.EnvironmentName == "" || verifyApiKeyReq.Key == "" { // TODO : set correct missing fields in error response errorResponse, _ := json.Marshal(errorResponse("Bad_REQUEST", "Missing element", http.StatusBadRequest)) @@ -232,7 +233,6 @@ errorCode = "oauth.v2.InvalidApiKeyForGivenResource" } - // TODO : empty check if verifyApiKeyReq.ValidateAgainstApiProxiesAndEnvs && (len(apiProductDetails.Apiproxies) > 0 && !contains(apiProductDetails.Apiproxies, verifyApiKeyReq.ApiProxyName)) { reason = "Proxy Validation Failed (" + strings.Join(apiProductDetails.Apiproxies, ", ") + " vs " + verifyApiKeyReq.ApiProxyName + ")" errorCode = "oauth.v2.InvalidApiKeyForGivenResource"
diff --git a/api_ShortListApiProduct_test.go b/api_ShortListApiProduct_test.go index f41e118..72c999e 100644 --- a/api_ShortListApiProduct_test.go +++ b/api_ShortListApiProduct_test.go
@@ -14,119 +14,153 @@ package apidVerifyApiKey -import "testing" +import ( + . "github.com/onsi/ginkgo" + . "github.com/onsi/gomega" +) -var shortListApiProductTestData = []struct { - testDesc string - req VerifyApiKeyRequest - dbData []ApiProductDetails - expectedResult string - expectedWhenValidateProxyEnvIsTrue string -}{ - { - testDesc: "single-product-happy-path", - req: VerifyApiKeyRequest{EnvironmentName: "test", ApiProxyName: "test-proxy", UriPath: "/this-is-my-path"}, - dbData: []ApiProductDetails{ApiProductDetails{Id: "api-product-1", Environments: []string{"test", "prod"}, Apiproxies: []string{"test-proxy"}, Resources: []string{"/**"}}}, - expectedResult: "api-product-1", - expectedWhenValidateProxyEnvIsTrue: "api-product-1", - }, - { - testDesc: "multi-product-custom-resource-happy-path", - req: VerifyApiKeyRequest{EnvironmentName: "test", ApiProxyName: "test-proxy", UriPath: "/this-is-my-path"}, - dbData: []ApiProductDetails{ - ApiProductDetails{Id: "api-product-1", Environments: []string{"test", "prod"}, Apiproxies: []string{"test-proxy"}, Resources: []string{"/a/**"}}, - ApiProductDetails{Id: "api-product-2", Environments: []string{"test", "prod"}, Apiproxies: []string{"test-proxy"}, Resources: []string{"/**"}}, - }, - expectedResult: "api-product-2", - expectedWhenValidateProxyEnvIsTrue: "api-product-2", - }, { - testDesc: "multi-product-only-one-matches-env", - req: VerifyApiKeyRequest{EnvironmentName: "stage", ApiProxyName: "test-proxy", UriPath: "/this-is-my-path"}, - dbData: []ApiProductDetails{ - ApiProductDetails{Id: "api-product-1", Environments: []string{"test", "prod"}, Apiproxies: []string{"test-proxy"}, Resources: []string{"/a/**"}}, - ApiProductDetails{Id: "api-product-2", Environments: []string{"test", "prod"}, Apiproxies: []string{"test-proxy"}, Resources: []string{"/**"}}, - ApiProductDetails{Id: "api-product-3", Environments: []string{"test", "prod", "stage"}, Apiproxies: []string{"test-proxy"}, Resources: []string{"/**"}}, - }, - expectedResult: "api-product-3", - expectedWhenValidateProxyEnvIsTrue: "api-product-3", - }, - { - testDesc: "multi-product-match-with-no-env", - req: VerifyApiKeyRequest{EnvironmentName: "stage", ApiProxyName: "test-proxy", UriPath: "/this-is-my-path"}, - dbData: []ApiProductDetails{ - ApiProductDetails{Id: "api-product-1", Environments: []string{"test", "prod"}, Apiproxies: []string{"test-proxy"}, Resources: []string{"/a/**"}}, - ApiProductDetails{Id: "api-product-2", Environments: []string{"test", "prod"}, Apiproxies: []string{"test-proxy"}, Resources: []string{"/**"}}, - ApiProductDetails{Id: "api-product-3", Environments: []string{}, Apiproxies: []string{"test-proxy"}, Resources: []string{"/**"}}, - }, - expectedResult: "api-product-3", - expectedWhenValidateProxyEnvIsTrue: "api-product-3", - }, - { - testDesc: "multi-product-match-env", - req: VerifyApiKeyRequest{EnvironmentName: "stage", ApiProxyName: "test-proxy", UriPath: "/this-is-my-path"}, - dbData: []ApiProductDetails{ - ApiProductDetails{Id: "api-product-1", Environments: []string{"test", "prod"}, Apiproxies: []string{"test-proxy"}, Resources: []string{"/a/**"}}, - ApiProductDetails{Id: "api-product-2", Environments: []string{"test", "prod", "stage"}, Apiproxies: []string{"test-proxy"}, Resources: []string{"/this-is-my-path"}}, - ApiProductDetails{Id: "api-product-3", Environments: []string{}, Apiproxies: []string{"test-proxy"}, Resources: []string{"/**"}}, - }, +var _ = Describe("verifyApiKey_shortListApiProduct", func() { - expectedResult: "api-product-2", - expectedWhenValidateProxyEnvIsTrue: "api-product-2", - }, + Context("shortListApiProduct tests", func() { + It("single-product-happy-path", func() { + td := shortListApiProductTestDataStruct{ + req: VerifyApiKeyRequest{EnvironmentName: "test", ApiProxyName: "test-proxy", UriPath: "/this-is-my-path"}, + dbData: []ApiProductDetails{ApiProductDetails{Id: "api-product-1", Environments: []string{"test", "prod"}, Apiproxies: []string{"test-proxy"}, Resources: []string{"/**"}}}, + expectedResult: "api-product-1", + } - { - testDesc: "multi-product-match-empty-res-env-proxy", - req: VerifyApiKeyRequest{EnvironmentName: "stage", ApiProxyName: "test-proxy", UriPath: "/this-is-my-path"}, - dbData: []ApiProductDetails{ - ApiProductDetails{Id: "api-product-1"}, - ApiProductDetails{Id: "api-product-2", Environments: []string{"test", "prod", "stage"}, Apiproxies: []string{"test-proxy"}, Resources: []string{"/this-is-my-path"}}, - ApiProductDetails{Id: "api-product-3", Environments: []string{}, Apiproxies: []string{"test-proxy"}, Resources: []string{"/**"}}, - }, + actual := shortListApiProduct(td.dbData, td.req) + Expect(actual.Id).Should(Equal(td.expectedResult)) + }) + It("multi-product-custom-resource-happy-path", func() { + td := shortListApiProductTestDataStruct{ - expectedResult: "api-product-1", - expectedWhenValidateProxyEnvIsTrue: "api-product-1", - }, + req: VerifyApiKeyRequest{EnvironmentName: "test", ApiProxyName: "test-proxy", UriPath: "/this-is-my-path"}, + dbData: []ApiProductDetails{ + ApiProductDetails{Id: "api-product-1", Environments: []string{"test", "prod"}, Apiproxies: []string{"test-proxy"}, Resources: []string{"/a/**"}}, + ApiProductDetails{Id: "api-product-2", Environments: []string{"test", "prod"}, Apiproxies: []string{"test-proxy"}, Resources: []string{"/**"}}, + }, + expectedResult: "api-product-2", + } - { - testDesc: "multi-product-match-empty-res-env-proxy-second-indexed", - req: VerifyApiKeyRequest{EnvironmentName: "stage", ApiProxyName: "test-proxy", UriPath: "/this-is-my-path"}, - dbData: []ApiProductDetails{ - ApiProductDetails{Id: "api-product-1", Environments: []string{"test", "prod"}, Apiproxies: []string{"test-proxy"}, Resources: []string{"/a/**"}}, - ApiProductDetails{Id: "api-product-2"}, - ApiProductDetails{Id: "api-product-3", Environments: []string{}, Apiproxies: []string{"test-proxy"}, Resources: []string{"/**"}}, - }, - expectedResult: "api-product-2", - expectedWhenValidateProxyEnvIsTrue: "api-product-2", - }, - { - testDesc: "multi-product-with-no-resource-match", - req: VerifyApiKeyRequest{EnvironmentName: "stage", ApiProxyName: "test-proxy", UriPath: "/this-is-my-path"}, - dbData: []ApiProductDetails{ - ApiProductDetails{Id: "api-product-1", Environments: []string{"test", "prod"}, Apiproxies: []string{"test-proxy"}, Resources: []string{"/a/**"}}, - ApiProductDetails{Id: "api-product-2"}, - ApiProductDetails{Id: "api-product-3", Environments: []string{}, Apiproxies: []string{"test-proxy"}, Resources: []string{"/b/**"}}, - }, - expectedResult: "api-product-2", - expectedWhenValidateProxyEnvIsTrue: "api-product-2", - }, - { - testDesc: "multi-product-non-existent-proxy", - req: VerifyApiKeyRequest{EnvironmentName: "stage", ApiProxyName: "test-non-exisitent-proxy", UriPath: "/this-is-my-path"}, - dbData: []ApiProductDetails{ - ApiProductDetails{Id: "api-product-1", Environments: []string{"test", "prod"}, Apiproxies: []string{"test-proxy"}, Resources: []string{"/a/**"}}, - ApiProductDetails{Id: "api-product-2", Environments: []string{"test", "prod"}, Apiproxies: []string{"test-proxy"}, Resources: []string{"/**"}}, - ApiProductDetails{Id: "api-product-3", Environments: []string{"test", "prod"}, Apiproxies: []string{"test-proxy"}, Resources: []string{"/b/**"}}, - }, - expectedResult: "api-product-2", - expectedWhenValidateProxyEnvIsTrue: "", - }, -} + actual := shortListApiProduct(td.dbData, td.req) + Expect(actual.Id).Should(Equal(td.expectedResult)) + }) + It("multi-product-only-one-matches-env", func() { + td := shortListApiProductTestDataStruct{ -func TestShortListApiProduct(t *testing.T) { - for _, td := range shortListApiProductTestData { - actual := shortListApiProduct(td.dbData, td.req) - if actual.Id != td.expectedResult { - t.Errorf("TestData (%s) ValidateProxyEnv (%t) : expected (%s), actual (%s)", td.testDesc, td.req.ValidateAgainstApiProxiesAndEnvs, td.expectedResult, actual.Id) - } - } + req: VerifyApiKeyRequest{EnvironmentName: "stage", ApiProxyName: "test-proxy", UriPath: "/this-is-my-path"}, + dbData: []ApiProductDetails{ + ApiProductDetails{Id: "api-product-1", Environments: []string{"test", "prod"}, Apiproxies: []string{"test-proxy"}, Resources: []string{"/a/**"}}, + ApiProductDetails{Id: "api-product-2", Environments: []string{"test", "prod"}, Apiproxies: []string{"test-proxy"}, Resources: []string{"/**"}}, + ApiProductDetails{Id: "api-product-3", Environments: []string{"test", "prod", "stage"}, Apiproxies: []string{"test-proxy"}, Resources: []string{"/**"}}, + }, + expectedResult: "api-product-3", + } + + actual := shortListApiProduct(td.dbData, td.req) + Expect(actual.Id).Should(Equal(td.expectedResult)) + }) + It("multi-product-match-with-no-env", func() { + td := shortListApiProductTestDataStruct{ + + req: VerifyApiKeyRequest{EnvironmentName: "stage", ApiProxyName: "test-proxy", UriPath: "/this-is-my-path"}, + dbData: []ApiProductDetails{ + ApiProductDetails{Id: "api-product-1", Environments: []string{"test", "prod"}, Apiproxies: []string{"test-proxy"}, Resources: []string{"/a/**"}}, + ApiProductDetails{Id: "api-product-2", Environments: []string{"test", "prod"}, Apiproxies: []string{"test-proxy"}, Resources: []string{"/**"}}, + ApiProductDetails{Id: "api-product-3", Environments: []string{}, Apiproxies: []string{"test-proxy"}, Resources: []string{"/**"}}, + }, + expectedResult: "api-product-3", + } + + actual := shortListApiProduct(td.dbData, td.req) + Expect(actual.Id).Should(Equal(td.expectedResult)) + }) + It("multi-product-match-env", func() { + td := shortListApiProductTestDataStruct{ + + req: VerifyApiKeyRequest{EnvironmentName: "stage", ApiProxyName: "test-proxy", UriPath: "/this-is-my-path"}, + dbData: []ApiProductDetails{ + ApiProductDetails{Id: "api-product-1", Environments: []string{"test", "prod"}, Apiproxies: []string{"test-proxy"}, Resources: []string{"/a/**"}}, + ApiProductDetails{Id: "api-product-2", Environments: []string{"test", "prod", "stage"}, Apiproxies: []string{"test-proxy"}, Resources: []string{"/this-is-my-path"}}, + ApiProductDetails{Id: "api-product-3", Environments: []string{}, Apiproxies: []string{"test-proxy"}, Resources: []string{"/**"}}, + }, + + expectedResult: "api-product-2", + } + + actual := shortListApiProduct(td.dbData, td.req) + Expect(actual.Id).Should(Equal(td.expectedResult)) + }) + + It("multi-product-match-empty-res-env-proxy", func() { + td := shortListApiProductTestDataStruct{ + + req: VerifyApiKeyRequest{EnvironmentName: "stage", ApiProxyName: "test-proxy", UriPath: "/this-is-my-path"}, + dbData: []ApiProductDetails{ + ApiProductDetails{Id: "api-product-1"}, + ApiProductDetails{Id: "api-product-2", Environments: []string{"test", "prod", "stage"}, Apiproxies: []string{"test-proxy"}, Resources: []string{"/this-is-my-path"}}, + ApiProductDetails{Id: "api-product-3", Environments: []string{}, Apiproxies: []string{"test-proxy"}, Resources: []string{"/**"}}, + }, + + expectedResult: "api-product-1", + } + + actual := shortListApiProduct(td.dbData, td.req) + Expect(actual.Id).Should(Equal(td.expectedResult)) + }) + + It("multi-product-match-empty-res-env-proxy-second-indexed", func() { + td := shortListApiProductTestDataStruct{ + + req: VerifyApiKeyRequest{EnvironmentName: "stage", ApiProxyName: "test-proxy", UriPath: "/this-is-my-path"}, + dbData: []ApiProductDetails{ + ApiProductDetails{Id: "api-product-1", Environments: []string{"test", "prod"}, Apiproxies: []string{"test-proxy"}, Resources: []string{"/a/**"}}, + ApiProductDetails{Id: "api-product-2"}, + ApiProductDetails{Id: "api-product-3", Environments: []string{}, Apiproxies: []string{"test-proxy"}, Resources: []string{"/**"}}, + }, + expectedResult: "api-product-2", + } + + actual := shortListApiProduct(td.dbData, td.req) + Expect(actual.Id).Should(Equal(td.expectedResult)) + }) + It("multi-product-with-no-resource-match", func() { + td := shortListApiProductTestDataStruct{ + + req: VerifyApiKeyRequest{EnvironmentName: "stage", ApiProxyName: "test-proxy", UriPath: "/this-is-my-path"}, + dbData: []ApiProductDetails{ + ApiProductDetails{Id: "api-product-1", Environments: []string{"test", "prod"}, Apiproxies: []string{"test-proxy"}, Resources: []string{"/a/**"}}, + ApiProductDetails{Id: "api-product-2"}, + ApiProductDetails{Id: "api-product-3", Environments: []string{}, Apiproxies: []string{"test-proxy"}, Resources: []string{"/b/**"}}, + }, + expectedResult: "api-product-2", + } + + actual := shortListApiProduct(td.dbData, td.req) + Expect(actual.Id).Should(Equal(td.expectedResult)) + }) + It("multi-product-non-existent-proxy", func() { + td := shortListApiProductTestDataStruct{ + + req: VerifyApiKeyRequest{EnvironmentName: "stage", ApiProxyName: "test-non-exisitent-proxy", UriPath: "/this-is-my-path"}, + dbData: []ApiProductDetails{ + ApiProductDetails{Id: "api-product-1", Environments: []string{"test", "prod"}, Apiproxies: []string{"test-proxy"}, Resources: []string{"/a/**"}}, + ApiProductDetails{Id: "api-product-2", Environments: []string{"test", "prod"}, Apiproxies: []string{"test-proxy"}, Resources: []string{"/**"}}, + ApiProductDetails{Id: "api-product-3", Environments: []string{"test", "prod"}, Apiproxies: []string{"test-proxy"}, Resources: []string{"/b/**"}}, + }, + expectedResult: "api-product-2", + } + + actual := shortListApiProduct(td.dbData, td.req) + Expect(actual.Id).Should(Equal(td.expectedResult)) + }) + }) + +}) + +type shortListApiProductTestDataStruct struct { + req VerifyApiKeyRequest + dbData []ApiProductDetails + expectedResult string }
diff --git a/api_performValidations_test.go b/api_performValidations_test.go index c6e015d..cb97eb0 100644 --- a/api_performValidations_test.go +++ b/api_performValidations_test.go
@@ -18,339 +18,487 @@ "encoding/json" "github.com/30x/apid-core" "github.com/30x/apid-core/factory" - "testing" + . "github.com/onsi/ginkgo" + . "github.com/onsi/gomega" ) -var performValidationsTestData = []struct { +type performValidationsTestDataStruct struct { testDesc string dataWrapper VerifyApiKeyRequestResponseDataWrapper expectedResult string expectedWhenValidateProxyEnvIsTrue string -}{ - { - testDesc: "happy-path", - expectedResult: "", - expectedWhenValidateProxyEnvIsTrue: "", - dataWrapper: VerifyApiKeyRequestResponseDataWrapper{ - verifyApiKeyRequest: VerifyApiKeyRequest{ - Key: "test-key", - OrganizationName: "test-org", - UriPath: "/test", - ApiProxyName: "test-proxy-name", - EnvironmentName: "test-env-name", - }, - tempDeveloperDetails: DeveloperDetails{ - Status: "ACTIVE", - }, - verifyApiKeySuccessResponse: VerifyApiKeySuccessResponse{ - ApiProduct: ApiProductDetails{ - Id: "test-api-product", - Resources: []string{"/**"}, - Apiproxies: []string{"test-proxy-name"}, - Environments: []string{"test-env-name"}, - Status: "APPROVED", - }, - App: AppDetails{ - Status: "APPROVED", - }, - ClientId: ClientIdDetails{ - Status: "APPROVED", - }, - }, - }, - }, { - testDesc: "Inactive Developer", - expectedResult: "{\"response_code\":\"keymanagement.service.DeveloperStatusNotActive\",\"response_message\":\"API Key verify failed for (test-key, test-org)\"}", - expectedWhenValidateProxyEnvIsTrue: "{\"response_code\":\"keymanagement.service.DeveloperStatusNotActive\",\"response_message\":\"API Key verify failed for (test-key, test-org)\"}", - dataWrapper: VerifyApiKeyRequestResponseDataWrapper{ - verifyApiKeyRequest: VerifyApiKeyRequest{ - Key: "test-key", - OrganizationName: "test-org", - UriPath: "/test", - ApiProxyName: "test-proxy-name", - EnvironmentName: "test-env-name", - }, - tempDeveloperDetails: DeveloperDetails{ - Status: "INACTIVE", - }, - verifyApiKeySuccessResponse: VerifyApiKeySuccessResponse{ - ApiProduct: ApiProductDetails{ - Id: "test-api-product", - Resources: []string{"/**"}, - Apiproxies: []string{"test-proxy-name"}, - Environments: []string{"test-env-name"}, - Status: "APPROVED", - }, - App: AppDetails{ - Status: "APPROVED", - }, - ClientId: ClientIdDetails{ - Status: "APPROVED", - }, - }, - }, - }, - { - testDesc: "Revoked Client Id", - expectedResult: "{\"response_code\":\"oauth.v2.ApiKeyNotApproved\",\"response_message\":\"API Key verify failed for (test-key, test-org)\"}", - expectedWhenValidateProxyEnvIsTrue: "{\"response_code\":\"oauth.v2.ApiKeyNotApproved\",\"response_message\":\"API Key verify failed for (test-key, test-org)\"}", - dataWrapper: VerifyApiKeyRequestResponseDataWrapper{ - verifyApiKeyRequest: VerifyApiKeyRequest{ - Key: "test-key", - OrganizationName: "test-org", - UriPath: "/test", - ApiProxyName: "test-proxy-name", - EnvironmentName: "test-env-name", - }, - tempDeveloperDetails: DeveloperDetails{ - Status: "ACTIVE", - }, - verifyApiKeySuccessResponse: VerifyApiKeySuccessResponse{ - ApiProduct: ApiProductDetails{ - Id: "test-api-product", - Resources: []string{"/**"}, - Apiproxies: []string{"test-proxy-name"}, - Environments: []string{"test-env-name"}, - Status: "APPROVED", - }, - App: AppDetails{ - Status: "APPROVED", - }, - ClientId: ClientIdDetails{ - Status: "REVOKED", - }, - }, - }, - }, - { - testDesc: "Revoked App", - expectedResult: "{\"response_code\":\"keymanagement.service.invalid_client-app_not_approved\",\"response_message\":\"API Key verify failed for (test-key, test-org)\"}", - expectedWhenValidateProxyEnvIsTrue: "{\"response_code\":\"keymanagement.service.invalid_client-app_not_approved\",\"response_message\":\"API Key verify failed for (test-key, test-org)\"}", - dataWrapper: VerifyApiKeyRequestResponseDataWrapper{ - verifyApiKeyRequest: VerifyApiKeyRequest{ - Key: "test-key", - OrganizationName: "test-org", - UriPath: "/test", - ApiProxyName: "test-proxy-name", - EnvironmentName: "test-env-name", - }, - tempDeveloperDetails: DeveloperDetails{ - Status: "ACTIVE", - }, - verifyApiKeySuccessResponse: VerifyApiKeySuccessResponse{ - ApiProduct: ApiProductDetails{ - Id: "test-api-product", - Resources: []string{"/**"}, - Apiproxies: []string{"test-proxy-name"}, - Environments: []string{"test-env-name"}, - Status: "APPROVED", - }, - App: AppDetails{ - Status: "REVOKED", - }, - ClientId: ClientIdDetails{ - Status: "APPROVED", - }, - }, - }, - }, - { - testDesc: "Company Inactive", - expectedResult: "{\"response_code\":\"keymanagement.service.CompanyStatusNotActive\",\"response_message\":\"API Key verify failed for (test-key, test-org)\"}", - expectedWhenValidateProxyEnvIsTrue: "{\"response_code\":\"keymanagement.service.CompanyStatusNotActive\",\"response_message\":\"API Key verify failed for (test-key, test-org)\"}", - dataWrapper: VerifyApiKeyRequestResponseDataWrapper{ - ctype: "company", - verifyApiKeyRequest: VerifyApiKeyRequest{ - Key: "test-key", - OrganizationName: "test-org", - UriPath: "/test", - ApiProxyName: "test-proxy-name", - EnvironmentName: "test-env-name", - }, - tempDeveloperDetails: DeveloperDetails{ - Status: "INACTIVE", - }, - verifyApiKeySuccessResponse: VerifyApiKeySuccessResponse{ - ApiProduct: ApiProductDetails{ - Id: "test-api-product", - Resources: []string{"/**"}, - Apiproxies: []string{"test-proxy-name"}, - Environments: []string{"test-env-name"}, - Status: "APPROVED", - }, - App: AppDetails{ - Status: "APPROVED", - }, - ClientId: ClientIdDetails{ - Status: "APPROVED", - }, - }, - }, - }, - { - testDesc: "Product not resolved", - expectedResult: "{\"response_code\":\"oauth.v2.InvalidApiKeyForGivenResource\",\"response_message\":\"Path Validation Failed. Product not resolved\"}", - expectedWhenValidateProxyEnvIsTrue: "{\"response_code\":\"oauth.v2.InvalidApiKeyForGivenResource\",\"response_message\":\"Path Validation Failed. Product not resolved\"}", - dataWrapper: VerifyApiKeyRequestResponseDataWrapper{ - verifyApiKeyRequest: VerifyApiKeyRequest{ - Key: "test-key", - OrganizationName: "test-org", - UriPath: "/test", - ApiProxyName: "test-proxy-name", - EnvironmentName: "test-env-name", - }, - tempDeveloperDetails: DeveloperDetails{ - Status: "ACTIVE", - }, - verifyApiKeySuccessResponse: VerifyApiKeySuccessResponse{ - ApiProduct: ApiProductDetails{}, - App: AppDetails{ - Status: "APPROVED", - }, - ClientId: ClientIdDetails{ - Status: "APPROVED", - }, - }, - }, - }, - { - testDesc: "resources not configured in db", - expectedResult: "", - expectedWhenValidateProxyEnvIsTrue: "", - dataWrapper: VerifyApiKeyRequestResponseDataWrapper{ - verifyApiKeyRequest: VerifyApiKeyRequest{ - Key: "test-key", - OrganizationName: "test-org", - UriPath: "/test", - ApiProxyName: "test-proxy-name", - EnvironmentName: "test-env-name", - }, - tempDeveloperDetails: DeveloperDetails{ - Status: "ACTIVE", - }, - verifyApiKeySuccessResponse: VerifyApiKeySuccessResponse{ - ApiProduct: ApiProductDetails{ - Id: "test-api-product", - Resources: []string{}, - Apiproxies: []string{"test-proxy-name"}, - Environments: []string{"test-env-name"}, - Status: "APPROVED", - }, - App: AppDetails{ - Status: "APPROVED", - }, - ClientId: ClientIdDetails{ - Status: "APPROVED", - }, - }, - }, - }, - { - testDesc: "proxies not configured in db", - expectedResult: "", - expectedWhenValidateProxyEnvIsTrue: "", - dataWrapper: VerifyApiKeyRequestResponseDataWrapper{ - verifyApiKeyRequest: VerifyApiKeyRequest{ - Key: "test-key", - OrganizationName: "test-org", - UriPath: "/test", - ApiProxyName: "test-proxy-name", - EnvironmentName: "test-env-name", - }, - tempDeveloperDetails: DeveloperDetails{ - Status: "ACTIVE", - }, - verifyApiKeySuccessResponse: VerifyApiKeySuccessResponse{ - ApiProduct: ApiProductDetails{ - Id: "test-api-product", - Resources: []string{"/test"}, - Apiproxies: []string{}, - Environments: []string{"test-env-name"}, - Status: "APPROVED", - }, - App: AppDetails{ - Status: "APPROVED", - }, - ClientId: ClientIdDetails{ - Status: "APPROVED", - }, - }, - }, - }, - { - testDesc: "environments not configured in db", - expectedResult: "", - expectedWhenValidateProxyEnvIsTrue: "", - dataWrapper: VerifyApiKeyRequestResponseDataWrapper{ - verifyApiKeyRequest: VerifyApiKeyRequest{ - Key: "test-key", - OrganizationName: "test-org", - UriPath: "/test", - ApiProxyName: "test-proxy-name", - EnvironmentName: "test-env-name", - }, - tempDeveloperDetails: DeveloperDetails{ - Status: "ACTIVE", - }, - verifyApiKeySuccessResponse: VerifyApiKeySuccessResponse{ - ApiProduct: ApiProductDetails{ - Id: "test-api-product", - Resources: []string{"/test"}, - Apiproxies: []string{"test-proxy-name"}, - Environments: []string{}, - Status: "APPROVED", - }, - App: AppDetails{ - Status: "APPROVED", - }, - ClientId: ClientIdDetails{ - Status: "APPROVED", - }, - }, - }, - }, } -func TestPerformValidation(t *testing.T) { +var _ = Describe("performValidationsTest", func() { - // tODO : what is the right way to get this ? apid.Initialize(factory.DefaultServicesFactory()) log = factory.DefaultServicesFactory().Log() a := apiManager{} - for _, td := range performValidationsTestData { - actualObject := a.performValidations(td.dataWrapper) - var actual string - if actualObject != nil { - a, _ := json.Marshal(&actualObject) - actual = string(a) - } else { - actual = "" - } - if string(actual) != td.expectedResult { - t.Errorf("TestData (%s) ValidateProxyEnv (%t) : expected (%s), actual (%s)", td.testDesc, td.dataWrapper.verifyApiKeyRequest.ValidateAgainstApiProxiesAndEnvs, td.expectedResult, actual) - } - } -} -func TestPerformValidationValidateProxyEnv(t *testing.T) { + Context("performValidationsTest tests", func() { + It("happy-path", func() { + td := performValidationsTestDataStruct{ + expectedResult: "", + expectedWhenValidateProxyEnvIsTrue: "", + dataWrapper: VerifyApiKeyRequestResponseDataWrapper{ + verifyApiKeyRequest: VerifyApiKeyRequest{ + Key: "test-key", + OrganizationName: "test-org", + UriPath: "/test", + ApiProxyName: "test-proxy-name", + EnvironmentName: "test-env-name", + }, + tempDeveloperDetails: DeveloperDetails{ + Status: "ACTIVE", + }, + verifyApiKeySuccessResponse: VerifyApiKeySuccessResponse{ + ApiProduct: ApiProductDetails{ + Id: "test-api-product", + Resources: []string{"/**"}, + Apiproxies: []string{"test-proxy-name"}, + Environments: []string{"test-env-name"}, + Status: "APPROVED", + }, + App: AppDetails{ + Status: "APPROVED", + }, + ClientId: ClientIdDetails{ + Status: "APPROVED", + }, + }, + }, + } + actualObject := a.performValidations(td.dataWrapper) + var actual string + if actualObject != nil { + a, _ := json.Marshal(&actualObject) + actual = string(a) + } else { + actual = "" + } + Expect(actual).Should(Equal(td.expectedResult)) - // tODO : what is the right way to get this ? - apid.Initialize(factory.DefaultServicesFactory()) - log = factory.DefaultServicesFactory().Log() - a := apiManager{} - for _, td := range performValidationsTestData { - td.dataWrapper.verifyApiKeyRequest.ValidateAgainstApiProxiesAndEnvs = true - actualObject := a.performValidations(td.dataWrapper) - var actual string - if actualObject != nil { - a, _ := json.Marshal(&actualObject) - actual = string(a) - } else { - actual = "" - } - if string(actual) != td.expectedWhenValidateProxyEnvIsTrue { + td.dataWrapper.verifyApiKeyRequest.ValidateAgainstApiProxiesAndEnvs = true + actualObject = a.performValidations(td.dataWrapper) + if actualObject != nil { + a, _ := json.Marshal(&actualObject) + actual = string(a) + } else { + actual = "" + } + Expect(actual).Should(Equal(td.expectedResult)) + }) + It("Inactive Developer", func() { + td := performValidationsTestDataStruct{ + expectedResult: "{\"response_code\":\"keymanagement.service.DeveloperStatusNotActive\",\"response_message\":\"API Key verify failed for (test-key, test-org)\"}", + expectedWhenValidateProxyEnvIsTrue: "{\"response_code\":\"keymanagement.service.DeveloperStatusNotActive\",\"response_message\":\"API Key verify failed for (test-key, test-org)\"}", + dataWrapper: VerifyApiKeyRequestResponseDataWrapper{ + verifyApiKeyRequest: VerifyApiKeyRequest{ + Key: "test-key", + OrganizationName: "test-org", + UriPath: "/test", + ApiProxyName: "test-proxy-name", + EnvironmentName: "test-env-name", + }, + tempDeveloperDetails: DeveloperDetails{ + Status: "INACTIVE", + }, + verifyApiKeySuccessResponse: VerifyApiKeySuccessResponse{ + ApiProduct: ApiProductDetails{ + Id: "test-api-product", + Resources: []string{"/**"}, + Apiproxies: []string{"test-proxy-name"}, + Environments: []string{"test-env-name"}, + Status: "APPROVED", + }, + App: AppDetails{ + Status: "APPROVED", + }, + ClientId: ClientIdDetails{ + Status: "APPROVED", + }, + }, + }, + } + actualObject := a.performValidations(td.dataWrapper) + var actual string + if actualObject != nil { + a, _ := json.Marshal(&actualObject) + actual = string(a) + } else { + actual = "" + } + Expect(actual).Should(Equal(td.expectedResult)) - t.Errorf("TestData (%s) ValidateProxyEnv (%t) : expected (%s), actual (%s)", td.testDesc, td.dataWrapper.verifyApiKeyRequest.ValidateAgainstApiProxiesAndEnvs, td.expectedWhenValidateProxyEnvIsTrue, actual) - } - } -} + td.dataWrapper.verifyApiKeyRequest.ValidateAgainstApiProxiesAndEnvs = true + actualObject = a.performValidations(td.dataWrapper) + if actualObject != nil { + a, _ := json.Marshal(&actualObject) + actual = string(a) + } else { + actual = "" + } + Expect(actual).Should(Equal(td.expectedResult)) + }) + It("Revoked Client Id", func() { + td := performValidationsTestDataStruct{ + expectedResult: "{\"response_code\":\"oauth.v2.ApiKeyNotApproved\",\"response_message\":\"API Key verify failed for (test-key, test-org)\"}", + expectedWhenValidateProxyEnvIsTrue: "{\"response_code\":\"oauth.v2.ApiKeyNotApproved\",\"response_message\":\"API Key verify failed for (test-key, test-org)\"}", + dataWrapper: VerifyApiKeyRequestResponseDataWrapper{ + verifyApiKeyRequest: VerifyApiKeyRequest{ + Key: "test-key", + OrganizationName: "test-org", + UriPath: "/test", + ApiProxyName: "test-proxy-name", + EnvironmentName: "test-env-name", + }, + tempDeveloperDetails: DeveloperDetails{ + Status: "ACTIVE", + }, + verifyApiKeySuccessResponse: VerifyApiKeySuccessResponse{ + ApiProduct: ApiProductDetails{ + Id: "test-api-product", + Resources: []string{"/**"}, + Apiproxies: []string{"test-proxy-name"}, + Environments: []string{"test-env-name"}, + Status: "APPROVED", + }, + App: AppDetails{ + Status: "APPROVED", + }, + ClientId: ClientIdDetails{ + Status: "REVOKED", + }, + }, + }, + } + actualObject := a.performValidations(td.dataWrapper) + var actual string + if actualObject != nil { + a, _ := json.Marshal(&actualObject) + actual = string(a) + } else { + actual = "" + } + Expect(actual).Should(Equal(td.expectedResult)) + + td.dataWrapper.verifyApiKeyRequest.ValidateAgainstApiProxiesAndEnvs = true + actualObject = a.performValidations(td.dataWrapper) + if actualObject != nil { + a, _ := json.Marshal(&actualObject) + actual = string(a) + } else { + actual = "" + } + Expect(actual).Should(Equal(td.expectedResult)) + }) + It("Revoked App", func() { + td := performValidationsTestDataStruct{ + expectedResult: "{\"response_code\":\"keymanagement.service.invalid_client-app_not_approved\",\"response_message\":\"API Key verify failed for (test-key, test-org)\"}", + expectedWhenValidateProxyEnvIsTrue: "{\"response_code\":\"keymanagement.service.invalid_client-app_not_approved\",\"response_message\":\"API Key verify failed for (test-key, test-org)\"}", + dataWrapper: VerifyApiKeyRequestResponseDataWrapper{ + verifyApiKeyRequest: VerifyApiKeyRequest{ + Key: "test-key", + OrganizationName: "test-org", + UriPath: "/test", + ApiProxyName: "test-proxy-name", + EnvironmentName: "test-env-name", + }, + tempDeveloperDetails: DeveloperDetails{ + Status: "ACTIVE", + }, + verifyApiKeySuccessResponse: VerifyApiKeySuccessResponse{ + ApiProduct: ApiProductDetails{ + Id: "test-api-product", + Resources: []string{"/**"}, + Apiproxies: []string{"test-proxy-name"}, + Environments: []string{"test-env-name"}, + Status: "APPROVED", + }, + App: AppDetails{ + Status: "REVOKED", + }, + ClientId: ClientIdDetails{ + Status: "APPROVED", + }, + }, + }, + } + actualObject := a.performValidations(td.dataWrapper) + var actual string + if actualObject != nil { + a, _ := json.Marshal(&actualObject) + actual = string(a) + } else { + actual = "" + } + Expect(actual).Should(Equal(td.expectedResult)) + + td.dataWrapper.verifyApiKeyRequest.ValidateAgainstApiProxiesAndEnvs = true + actualObject = a.performValidations(td.dataWrapper) + if actualObject != nil { + a, _ := json.Marshal(&actualObject) + actual = string(a) + } else { + actual = "" + } + Expect(actual).Should(Equal(td.expectedResult)) + }) + It("Company Inactive", func() { + td := performValidationsTestDataStruct{ + expectedResult: "{\"response_code\":\"keymanagement.service.CompanyStatusNotActive\",\"response_message\":\"API Key verify failed for (test-key, test-org)\"}", + expectedWhenValidateProxyEnvIsTrue: "{\"response_code\":\"keymanagement.service.CompanyStatusNotActive\",\"response_message\":\"API Key verify failed for (test-key, test-org)\"}", + dataWrapper: VerifyApiKeyRequestResponseDataWrapper{ + ctype: "company", + verifyApiKeyRequest: VerifyApiKeyRequest{ + Key: "test-key", + OrganizationName: "test-org", + UriPath: "/test", + ApiProxyName: "test-proxy-name", + EnvironmentName: "test-env-name", + }, + tempDeveloperDetails: DeveloperDetails{ + Status: "INACTIVE", + }, + verifyApiKeySuccessResponse: VerifyApiKeySuccessResponse{ + ApiProduct: ApiProductDetails{ + Id: "test-api-product", + Resources: []string{"/**"}, + Apiproxies: []string{"test-proxy-name"}, + Environments: []string{"test-env-name"}, + Status: "APPROVED", + }, + App: AppDetails{ + Status: "APPROVED", + }, + ClientId: ClientIdDetails{ + Status: "APPROVED", + }, + }, + }, + } + actualObject := a.performValidations(td.dataWrapper) + var actual string + if actualObject != nil { + a, _ := json.Marshal(&actualObject) + actual = string(a) + } else { + actual = "" + } + Expect(actual).Should(Equal(td.expectedResult)) + + td.dataWrapper.verifyApiKeyRequest.ValidateAgainstApiProxiesAndEnvs = true + actualObject = a.performValidations(td.dataWrapper) + if actualObject != nil { + a, _ := json.Marshal(&actualObject) + actual = string(a) + } else { + actual = "" + } + Expect(actual).Should(Equal(td.expectedResult)) + }) + It("Product not resolved", func() { + td := performValidationsTestDataStruct{ + expectedResult: "{\"response_code\":\"oauth.v2.InvalidApiKeyForGivenResource\",\"response_message\":\"Path Validation Failed. Product not resolved\"}", + expectedWhenValidateProxyEnvIsTrue: "{\"response_code\":\"oauth.v2.InvalidApiKeyForGivenResource\",\"response_message\":\"Path Validation Failed. Product not resolved\"}", + dataWrapper: VerifyApiKeyRequestResponseDataWrapper{ + verifyApiKeyRequest: VerifyApiKeyRequest{ + Key: "test-key", + OrganizationName: "test-org", + UriPath: "/test", + ApiProxyName: "test-proxy-name", + EnvironmentName: "test-env-name", + }, + tempDeveloperDetails: DeveloperDetails{ + Status: "ACTIVE", + }, + verifyApiKeySuccessResponse: VerifyApiKeySuccessResponse{ + ApiProduct: ApiProductDetails{}, + App: AppDetails{ + Status: "APPROVED", + }, + ClientId: ClientIdDetails{ + Status: "APPROVED", + }, + }, + }, + } + actualObject := a.performValidations(td.dataWrapper) + var actual string + if actualObject != nil { + a, _ := json.Marshal(&actualObject) + actual = string(a) + } else { + actual = "" + } + Expect(actual).Should(Equal(td.expectedResult)) + + td.dataWrapper.verifyApiKeyRequest.ValidateAgainstApiProxiesAndEnvs = true + actualObject = a.performValidations(td.dataWrapper) + if actualObject != nil { + a, _ := json.Marshal(&actualObject) + actual = string(a) + } else { + actual = "" + } + Expect(actual).Should(Equal(td.expectedResult)) + }) + It("resources not configured in db", func() { + td := performValidationsTestDataStruct{ + expectedResult: "", + expectedWhenValidateProxyEnvIsTrue: "", + dataWrapper: VerifyApiKeyRequestResponseDataWrapper{ + verifyApiKeyRequest: VerifyApiKeyRequest{ + Key: "test-key", + OrganizationName: "test-org", + UriPath: "/test", + ApiProxyName: "test-proxy-name", + EnvironmentName: "test-env-name", + }, + tempDeveloperDetails: DeveloperDetails{ + Status: "ACTIVE", + }, + verifyApiKeySuccessResponse: VerifyApiKeySuccessResponse{ + ApiProduct: ApiProductDetails{ + Id: "test-api-product", + Resources: []string{}, + Apiproxies: []string{"test-proxy-name"}, + Environments: []string{"test-env-name"}, + Status: "APPROVED", + }, + App: AppDetails{ + Status: "APPROVED", + }, + ClientId: ClientIdDetails{ + Status: "APPROVED", + }, + }, + }, + } + actualObject := a.performValidations(td.dataWrapper) + var actual string + if actualObject != nil { + a, _ := json.Marshal(&actualObject) + actual = string(a) + } else { + actual = "" + } + Expect(actual).Should(Equal(td.expectedResult)) + + td.dataWrapper.verifyApiKeyRequest.ValidateAgainstApiProxiesAndEnvs = true + actualObject = a.performValidations(td.dataWrapper) + if actualObject != nil { + a, _ := json.Marshal(&actualObject) + actual = string(a) + } else { + actual = "" + } + Expect(actual).Should(Equal(td.expectedResult)) + }) + It("proxies not configured in db", func() { + td := performValidationsTestDataStruct{ + expectedResult: "", + expectedWhenValidateProxyEnvIsTrue: "", + dataWrapper: VerifyApiKeyRequestResponseDataWrapper{ + verifyApiKeyRequest: VerifyApiKeyRequest{ + Key: "test-key", + OrganizationName: "test-org", + UriPath: "/test", + ApiProxyName: "test-proxy-name", + EnvironmentName: "test-env-name", + }, + tempDeveloperDetails: DeveloperDetails{ + Status: "ACTIVE", + }, + verifyApiKeySuccessResponse: VerifyApiKeySuccessResponse{ + ApiProduct: ApiProductDetails{ + Id: "test-api-product", + Resources: []string{"/test"}, + Apiproxies: []string{}, + Environments: []string{"test-env-name"}, + Status: "APPROVED", + }, + App: AppDetails{ + Status: "APPROVED", + }, + ClientId: ClientIdDetails{ + Status: "APPROVED", + }, + }, + }, + } + actualObject := a.performValidations(td.dataWrapper) + var actual string + if actualObject != nil { + a, _ := json.Marshal(&actualObject) + actual = string(a) + } else { + actual = "" + } + Expect(actual).Should(Equal(td.expectedResult)) + + td.dataWrapper.verifyApiKeyRequest.ValidateAgainstApiProxiesAndEnvs = true + actualObject = a.performValidations(td.dataWrapper) + if actualObject != nil { + a, _ := json.Marshal(&actualObject) + actual = string(a) + } else { + actual = "" + } + Expect(actual).Should(Equal(td.expectedResult)) + }) + It("environments not configured in db", func() { + td := performValidationsTestDataStruct{ + expectedResult: "", + expectedWhenValidateProxyEnvIsTrue: "", + dataWrapper: VerifyApiKeyRequestResponseDataWrapper{ + verifyApiKeyRequest: VerifyApiKeyRequest{ + Key: "test-key", + OrganizationName: "test-org", + UriPath: "/test", + ApiProxyName: "test-proxy-name", + EnvironmentName: "test-env-name", + }, + tempDeveloperDetails: DeveloperDetails{ + Status: "ACTIVE", + }, + verifyApiKeySuccessResponse: VerifyApiKeySuccessResponse{ + ApiProduct: ApiProductDetails{ + Id: "test-api-product", + Resources: []string{"/test"}, + Apiproxies: []string{"test-proxy-name"}, + Environments: []string{}, + Status: "APPROVED", + }, + App: AppDetails{ + Status: "APPROVED", + }, + ClientId: ClientIdDetails{ + Status: "APPROVED", + }, + }, + }, + } + actualObject := a.performValidations(td.dataWrapper) + var actual string + if actualObject != nil { + a, _ := json.Marshal(&actualObject) + actual = string(a) + } else { + actual = "" + } + Expect(actual).Should(Equal(td.expectedResult)) + + td.dataWrapper.verifyApiKeyRequest.ValidateAgainstApiProxiesAndEnvs = true + actualObject = a.performValidations(td.dataWrapper) + if actualObject != nil { + a, _ := json.Marshal(&actualObject) + actual = string(a) + } else { + actual = "" + } + Expect(actual).Should(Equal(td.expectedResult)) + }) + + }) +})
diff --git a/api_test.go b/api_test.go new file mode 100644 index 0000000..5b1df1d --- /dev/null +++ b/api_test.go
@@ -0,0 +1,20 @@ +// Copyright 2017 Google Inc. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +package apidVerifyApiKey + +// TODO: end to end IT tests +// 1. happy path for developer +// 2. happy path for company +// 3. error case for developer / company +// 4. input request validation error case
diff --git a/data.go b/data.go index 6270b43..dc9ea93 100644 --- a/data.go +++ b/data.go
@@ -22,9 +22,10 @@ ) type dbManager struct { - data apid.DataService - db apid.DB - dbMux sync.RWMutex + data apid.DataService + db apid.DB + dbMux sync.RWMutex + dbVersion string } func (dbc *dbManager) setDbVersion(version string) { @@ -35,6 +36,8 @@ dbc.dbMux.Lock() dbc.db = db dbc.dbMux.Unlock() + dbc.dbVersion = version + // TODO : check if we need to release old db here... } func (dbc *dbManager) getDb() apid.DB { @@ -51,10 +54,15 @@ return nil } +func (dbc *dbManager) getDbVersion() string { + return dbc.dbVersion +} + type dbManagerInterface interface { setDbVersion(string) initDb() error getDb() apid.DB + getDbVersion() string getKmsAttributes(tenantId string, entities ...string) map[string][]Attribute getApiKeyDetails(dataWrapper *VerifyApiKeyRequestResponseDataWrapper) error } @@ -94,7 +102,7 @@ db := dbc.db - err := db.QueryRow(sql_GET_API_KEY_DETAILS_SQL , dataWrapper.verifyApiKeyRequest.Key, dataWrapper.verifyApiKeyRequest.OrganizationName). + err := db.QueryRow(sql_GET_API_KEY_DETAILS_SQL, dataWrapper.verifyApiKeyRequest.Key, dataWrapper.verifyApiKeyRequest.OrganizationName). Scan( &dataWrapper.ctype, &dataWrapper.tenant_id,
diff --git a/data_test.go b/data_test.go new file mode 100644 index 0000000..1d20e31 --- /dev/null +++ b/data_test.go
@@ -0,0 +1,67 @@ +// Copyright 2017 Google Inc. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +package apidVerifyApiKey + +import ( + "github.com/30x/apid-core" + . "github.com/onsi/gomega" +) + +// TODO: sql tests +// 1. get api key sql test.. verify all fields, json to array conversions +// 2. get api - no row should return proper error +// 3. get attributes query tests +// 4. get attributes with all empty results returned +// 5. get product with no results +// 6. get product with status != approved +// 7. get products happy path + +//initialize DB for tests +func initTestDb(db apid.DB) { + _, err := db.Exec(`CREATE TABLE kms_organization (id text,name text,display_name text,type text,tenant_id text,customer_id text,description text,created_at blob,created_by text,updated_at blob,updated_by text,_change_selector text, primary key (id,tenant_id));`) + Expect(err).Should(Succeed()) + _, err = db.Exec(`INSERT INTO "kms_organization" VALUES('85629786-37c5-4e8c-bb45-208f3360d005','apigee-mcrosrvc-client0001','apigee-mcrosrvc-client0001','trial','bc811169','2277ba6c-8991-4a38-a5fc-12d8d36e5812','','2017-07-03 19:21:09.388+00:00','defaultUser','2017-07-05 16:24:35.413+00:00','rajanish@apigee.com','bc811169');`) + Expect(err).Should(Succeed()) + _, err = db.Exec(`CREATE TABLE kms_company (id text,tenant_id text,name text,display_name text,status text,created_at blob,created_by text,updated_at blob,updated_by text,_change_selector text, primary key (id,tenant_id));`) + Expect(err).Should(Succeed()) + _, err = db.Exec(`INSERT INTO "kms_company" VALUES('7834c683-9453-4389-b816-34ca24dfccd9','bc811169','DevCompany','East India Company','ACTIVE','2017-08-05 19:54:12.359+00:00','defaultUser','2017-08-05 19:54:12.359+00:00','defaultUser','bc811169');`) + Expect(err).Should(Succeed()) + _, err = db.Exec(``) + Expect(err).Should(Succeed()) + _, err = db.Exec(`CREATE TABLE kms_app (id text,tenant_id text,name text,display_name text,access_type text,callback_url text,status text,app_family text,company_id text,developer_id text,parent_id text,type text,created_at blob,created_by text,updated_at blob,updated_by text,_change_selector text, primary key (id,tenant_id));`) + Expect(err).Should(Succeed()) + _, err = db.Exec(`INSERT INTO "kms_app" VALUES('d371f05a-7c04-430c-b12d-26cf4e4d5d65','bc811169','CompApp2','','READ','www.apple.com','APPROVED','default','7834c683-9453-4389-b816-34ca24dfccd9','','7834c683-9453-4389-b816-34ca24dfccd9','COMPANY','2017-08-07 17:00:54.25+00:00','defaultUser','2017-08-07 17:09:08.259+00:00','defaultUser','bc811169');`) + Expect(err).Should(Succeed()) + _, err = db.Exec(``) + Expect(err).Should(Succeed()) + _, err = db.Exec(`CREATE TABLE kms_app_credential (id text,tenant_id text,consumer_secret text,app_id text,method_type text,status text,issued_at blob,expires_at blob,app_status text,scopes text,created_at blob,created_by text,updated_at blob,updated_by text,_change_selector text, primary key (id,tenant_id));`) + Expect(err).Should(Succeed()) + _, err = db.Exec(`INSERT INTO "kms_app_credential" VALUES('63tHSNLKJkcc6GENVWGT1Zw5gek7kVJ0','bc811169','Ui8dcyGW3lA04YdX','d371f05a-7c04-430c-b12d-26cf4e4d5d65','','APPROVED','2017-08-07 17:00:54.258+00:00','','','{DELETE}','2017-08-07 17:00:54.258+00:00','-NA-','2017-08-07 17:06:06.242+00:00','-NA-','bc811169');`) + Expect(err).Should(Succeed()) + _, err = db.Exec(``) + Expect(err).Should(Succeed()) + _, err = db.Exec(`CREATE TABLE kms_app_credential_apiproduct_mapper (tenant_id text,appcred_id text,app_id text,apiprdt_id text,status text,_change_selector text, primary key (tenant_id,appcred_id,app_id,apiprdt_id));`) + Expect(err).Should(Succeed()) + _, err = db.Exec(`INSERT INTO "kms_app_credential_apiproduct_mapper" VALUES('bc811169','63tHSNLKJkcc6GENVWGT1Zw5gek7kVJ0','d371f05a-7c04-430c-b12d-26cf4e4d5d65','b6c9fa49-35d6-48b2-b5f5-99dd3953bd18','APPROVED','bc811169');`) + Expect(err).Should(Succeed()) + _, err = db.Exec(``) + Expect(err).Should(Succeed()) + _, err = db.Exec(`CREATE TABLE kms_attributes (tenant_id text,entity_id text,cust_id text,org_id text,dev_id text,comp_id text,apiprdt_id text,app_id text,appcred_id text,name text,type text,value text,_change_selector text, primary key (tenant_id,entity_id,name,type));`) + Expect(err).Should(Succeed()) + _, err = db.Exec(`INSERT INTO "kms_attributes" VALUES('bc811169','d371f05a-7c04-430c-b12d-26cf4e4d5d65','','','','','','d371f05a-7c04-430c-b12d-26cf4e4d5d65','','Company','APP','Apple','bc811169');`) + Expect(err).Should(Succeed()) + _, err = db.Exec(`INSERT INTO "kms_attributes" VALUES('bc811169','7834c683-9453-4389-b816-34ca24dfccd9','','','','7834c683-9453-4389-b816-34ca24dfccd9','','','','country','COMPANY','england','bc811169');`) + Expect(err).Should(Succeed()) + +}
diff --git a/listener.go b/listener.go index 416c557..2d2077e 100644 --- a/listener.go +++ b/listener.go
@@ -26,7 +26,6 @@ type apigeeSyncHandler struct { dbMan dbManagerInterface apiMan apiManager - closed bool } func (h *apigeeSyncHandler) initListener(services apid.Services) {
diff --git a/listener_test.go b/listener_test.go index dc3cd34..78ea736 100644 --- a/listener_test.go +++ b/listener_test.go
@@ -16,63 +16,75 @@ import ( "github.com/30x/apid-core" + "github.com/30x/apid-core/factory" "github.com/apigee-labs/transicator/common" . "github.com/onsi/ginkgo" - //. "github.com/onsi/gomega" - //"github.com/30x/apid-core/data" + . "github.com/onsi/gomega" + "io/ioutil" + "os" + "sync" ) var _ = Describe("listener", func() { - Context("KMS create/updates verification via changes for Developer", func() { + var listnerTestSyncHandler apigeeSyncHandler + var listnerTestTempDir string + var _ = BeforeEach(func() { + var err error + listnerTestTempDir, err = ioutil.TempDir("", "listner_test") + s := factory.DefaultServicesFactory() + apid.Initialize(s) + config := apid.Config() + config.Set("data_path", listnerTestTempDir) + Expect(err).NotTo(HaveOccurred()) - handler := apigeeSyncHandler{} + apid.InitializePlugins("") + + db, err := apid.Data().DB() + Expect(err).NotTo(HaveOccurred()) + + dbMan := &dbManager{ + data: s.Data(), + dbMux: sync.RWMutex{}, + db: db, + } + dbMan.initDb() + + listnerTestSyncHandler = apigeeSyncHandler{ + dbMan: dbMan, + apiMan: apiManager{}, + } + + listnerTestSyncHandler.initListener(s) + }) + + var _ = AfterEach(func() { + os.RemoveAll(listnerTestTempDir) + }) + + Context("Apigee Sync Event Processing", func() { It("should set DB to appropriate version", func() { - - //saveDb := handler.dbMan.getDb() - s := &common.Snapshot{ SnapshotInfo: "test_snapshot", Tables: []common.Table{}, } + listnerTestSyncHandler.Handle(s) + Expect(listnerTestSyncHandler.dbMan.getDbVersion()).Should(BeEquivalentTo(s.SnapshotInfo)) - handler.Handle(s) + }) - //expectedDB, err := handler.dbMan.data.DBVersion(s.SnapshotInfo) - //Expect(err).NotTo(HaveOccurred()) - // - //Expect(getDB() == expectedDB).Should(BeTrue()) - // - ////restore the db to the valid one - //setDB(saveDb) + It("should not change version for chang event", func() { + + version := listnerTestSyncHandler.dbMan.getDbVersion() + s := &common.Change{ + ChangeSequence: 12321, + Table: "", + } + testSyncHandler.Handle(s) + Expect(listnerTestSyncHandler.dbMan.getDbVersion() == version).Should(BeTrue()) + }) }) }) - -func addScopes(db apid.DB) { - txn, _ := db.Begin() - txn.Exec("INSERT INTO EDGEX_DATA_SCOPE (id, _change_selector, apid_cluster_id, scope, org, env) "+ - "VALUES"+ - "($1,$2,$3,$4,$5,$6)", - "ABCDE", - "some_cluster_id", - "some_cluster_id", - "tenant_id_xxxx", - "test_org0", - "Env_0", - ) - txn.Exec("INSERT INTO EDGEX_DATA_SCOPE (id, _change_selector, apid_cluster_id, scope, org, env) "+ - "VALUES"+ - "($1,$2,$3,$4,$5,$6)", - "XYZ", - "test_org0", - "somecluster_id", - "tenant_id_0", - "test_org0", - "Env_0", - ) - log.Info("Inserted EDGEX_DATA_SCOPE for test") - txn.Commit() -}
diff --git a/sqlQueries.go b/sqlQueries.go index 609c3b3..3d121b0 100644 --- a/sqlQueries.go +++ b/sqlQueries.go
@@ -109,7 +109,6 @@ AND o.name = $2) ;` - const sql_GET_API_PRODUCTS_FOR_KEY_SQL = ` SELECT COALESCE(ap.id,"") as prod_id, @@ -131,12 +130,13 @@ ON mp.appcred_id = c.id INNER JOIN KMS_API_PRODUCT as ap ON ap.id = mp.apiprdt_id - WHERE (mp.apiprdt_id = ap.id + WHERE ( + mp.apiprdt_id = ap.id AND mp.appcred_id = c.id + AND mp.status = 'APPROVED' AND c.id = $1 AND ap.tenant_id = $2 - AND mp.status = 'APPROVED') + ) ;` - const sql_GET_KMS_ATTRIBUTES_FOR_TENANT = "select entity_id, name, value from kms_attributes where tenant_id = $1"
diff --git a/test_helper.go b/test_helper.go index de2db79..4c1a026 100644 --- a/test_helper.go +++ b/test_helper.go
@@ -252,3 +252,29 @@ log.Panic("Unable to initialize DB", err) } } + +func addScopes(db apid.DB) { + txn, _ := db.Begin() + txn.Exec("INSERT INTO EDGEX_DATA_SCOPE (id, _change_selector, apid_cluster_id, scope, org, env) "+ + "VALUES"+ + "($1,$2,$3,$4,$5,$6)", + "ABCDE", + "some_cluster_id", + "some_cluster_id", + "tenant_id_xxxx", + "test_org0", + "Env_0", + ) + txn.Exec("INSERT INTO EDGEX_DATA_SCOPE (id, _change_selector, apid_cluster_id, scope, org, env) "+ + "VALUES"+ + "($1,$2,$3,$4,$5,$6)", + "XYZ", + "test_org0", + "somecluster_id", + "tenant_id_0", + "test_org0", + "Env_0", + ) + log.Info("Inserted EDGEX_DATA_SCOPE for test") + txn.Commit() +}
diff --git a/validate_env_test.go b/validate_env_test.go deleted file mode 100644 index 024e26b..0000000 --- a/validate_env_test.go +++ /dev/null
@@ -1,40 +0,0 @@ -// Copyright 2017 Google Inc. -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -package apidVerifyApiKey - -import ( - . "github.com/onsi/ginkgo" - . "github.com/onsi/gomega" -) - -var _ = Describe("Validate Env", func() { - - It("validation1", func() { - s := contains([]string{"foo", "bar"}, "foo") - Expect(s).Should(BeTrue()) - }) - It("validation2", func() { - s := contains([]string{"foo", "bar"}, "bar") - Expect(s).Should(BeTrue()) - }) - It("validation3", func() { - s := contains([]string{"foo", "bar"}, "xxx") - Expect(s).Should(BeFalse()) - }) - It("validation4", func() { - s := contains([]string{}, "xxx") - Expect(s).Should(BeFalse()) - }) -})
diff --git a/verifyApiKeyUtil.go b/verifyApiKeyUtil.go index 16d0936..a7d2451 100644 --- a/verifyApiKeyUtil.go +++ b/verifyApiKeyUtil.go
@@ -58,7 +58,7 @@ func jsonToStringArray(fjson string) []string { var array []string - if err := json.Unmarshal([]byte(fjson), array); err == nil { + if err := json.Unmarshal([]byte(fjson), &array); err == nil { return array } s := strings.TrimPrefix(fjson, "{")
diff --git a/verifyApiKeyUtil_test.go b/verifyApiKeyUtil_test.go new file mode 100644 index 0000000..4e17289 --- /dev/null +++ b/verifyApiKeyUtil_test.go
@@ -0,0 +1,70 @@ +// Copyright 2017 Google Inc. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package apidVerifyApiKey + +import ( + . "github.com/onsi/ginkgo" + . "github.com/onsi/gomega" + "reflect" +) + +var _ = Describe("Validate Env", func() { + + It("validation1", func() { + s := contains([]string{"foo", "bar"}, "foo") + Expect(s).Should(BeTrue()) + }) + It("validation2", func() { + s := contains([]string{"foo", "bar"}, "bar") + Expect(s).Should(BeTrue()) + }) + It("validation3", func() { + s := contains([]string{"foo", "bar"}, "xxx") + Expect(s).Should(BeFalse()) + }) + It("validation4", func() { + s := contains([]string{}, "xxx") + Expect(s).Should(BeFalse()) + }) +}) + +var _ = Describe("Validate jsonToStringArray", func() { + + It("should tranform simple valid json", func() { + array := jsonToStringArray("[\"test-1\", \"test-2\"]") + Expect(reflect.DeepEqual(array, []string{"test-1", "test-2"})).Should(BeTrue()) + }) + It("should tranform simple single valid json", func() { + array := jsonToStringArray("[\"test-1\"]") + Expect(reflect.DeepEqual(array, []string{"test-1"})).Should(BeTrue()) + }) + It("should tranform simple fake json", func() { + s := jsonToStringArray("{test-1,test-2}") + Expect(reflect.DeepEqual(s, []string{"test-1", "test-2"})).Should(BeTrue()) + }) + It("should tranform simple single valued fake json", func() { + s := jsonToStringArray("{test-1}") + Expect(reflect.DeepEqual(s, []string{"test-1"})).Should(BeTrue()) + }) + It("space between fields considered as valid char", func() { + s := jsonToStringArray("{test-1, test-2}") + Expect(reflect.DeepEqual(s, []string{"test-1", " test-2"})).Should(BeTrue()) + }) + It("remove only last braces", func() { + s := jsonToStringArray("{test-1,test-2}}") + Expect(reflect.DeepEqual(s, []string{"test-1", "test-2}"})).Should(BeTrue()) + }) + +})
diff --git a/validate_path_test.go b/verifyApiKeyUtil_validate_path_test.go similarity index 78% rename from validate_path_test.go rename to verifyApiKeyUtil_validate_path_test.go index 563b52c..eb91d8f 100644 --- a/validate_path_test.go +++ b/verifyApiKeyUtil_validate_path_test.go
@@ -54,7 +54,7 @@ Expect(s).Should(BeTrue()) }) It("validation9", func() { - s := validatePath([]string{"{/foo/**"}, "/foo/bar") + s := validatePath([]string{"/foo/**"}, "/foo/bar") Expect(s).Should(BeTrue()) }) It("validation10", func() { @@ -62,7 +62,7 @@ Expect(s).Should(BeFalse()) }) It("validation11", func() { - s := validatePath([]string{"{/foo/bar/**"}, "/foo/bar/xx/yy") + s := validatePath([]string{"/foo/bar/**"}, "/foo/bar/xx/yy") Expect(s).Should(BeTrue()) }) It("validation12", func() { @@ -70,39 +70,39 @@ Expect(s).Should(BeTrue()) }) It("validation13", func() { - s := validatePath([]string{"{/foo/bar/*/"}, "/foo/bar/xxx") + s := validatePath([]string{"/foo/bar/*/"}, "/foo/bar/xxx") Expect(s).Should(BeFalse()) }) It("validation14", func() { - s := validatePath([]string{"{/foo/bar/**"}, "/foo/bar/xx/yy") + s := validatePath([]string{"/foo/bar/**"}, "/foo/bar/xx/yy") Expect(s).Should(BeTrue()) }) It("validation15", func() { - s := validatePath([]string{"{/foo/*/**/"}, "/foo/bar") + s := validatePath([]string{"/foo/*/**/"}, "/foo/bar") Expect(s).Should(BeFalse()) }) It("validation16", func() { - s := validatePath([]string{"{/foo/bar/*/xxx"}, "/foo/bar/yyy/xxx") + s := validatePath([]string{"/foo/bar/*/xxx"}, "/foo/bar/yyy/xxx") Expect(s).Should(BeTrue()) }) It("validation17", func() { - s := validatePath([]string{"{/foo/bar/*/xxx/"}, "/foo/bar/yyy/xxx") + s := validatePath([]string{"/foo/bar/*/xxx/"}, "/foo/bar/yyy/xxx") Expect(s).Should(BeFalse()) }) It("validation18", func() { - s := validatePath([]string{"{/foo/bar/**/xxx/"}, "/foo/bar/aaa/bbb/xxx/") + s := validatePath([]string{"/foo/bar/**/xxx/"}, "/foo/bar/aaa/bbb/xxx/") Expect(s).Should(BeTrue()) }) It("validation19", func() { - s := validatePath([]string{"{/foo/bar/***/xxx/"}, "/foo/bar/aaa/bbb/xxx/") + s := validatePath([]string{"/foo/bar/***/xxx/"}, "/foo/bar/aaa/bbb/xxx/") Expect(s).Should(BeTrue()) }) It("validation20", func() { - s := validatePath([]string{"{/foo/, /bar/"}, "/foo/") + s := validatePath([]string{"/foo/", "/bar/"}, "/foo/") Expect(s).Should(BeTrue()) }) It("validation21", func() { - s := validatePath([]string{"{/foo/bar/yy*/xxx"}, "/foo/bar/yyy/xxx") + s := validatePath([]string{"/foo/bar/yy*/xxx"}, "/foo/bar/yyy/xxx") Expect(s).Should(BeTrue()) }) })