Add more test cases for all KMS tables (INS/UPD/DEL) and fixed issues found while testing them
diff --git a/api_test.go b/api_test.go index 369d540..7a0b86a 100644 --- a/api_test.go +++ b/api_test.go
@@ -2,6 +2,7 @@ import ( "encoding/json" + "github.com/apigee-labs/transicator/common" . "github.com/onsi/ginkgo" . "github.com/onsi/gomega" "io/ioutil" @@ -13,14 +14,250 @@ var _ = Describe("api", func() { - Context("verifyAPIKey() directly", func() { + Context("DB Inserts/Deletes verification", func() { + + It("Positive DB test for Insert operations", func() { + db := getDB() + txn, err := db.Begin() + Expect(err).ShouldNot(HaveOccurred()) + + for i := 0; i < 10; i++ { + var rows []common.Row + srvItems := common.Row{} + result := strconv.FormatInt(int64(i), 10) + + scv := &common.ColumnVal{ + Value: "api_product_" + result, + Type: 1, + } + srvItems["id"] = scv + + scv = &common.ColumnVal{ + Value: "{/**, /test}", + Type: 1, + } + srvItems["api_resources"] = scv + + scv = &common.ColumnVal{ + Value: "{Env_0, Env_1}", + Type: 1, + } + srvItems["environments"] = scv + + scv = &common.ColumnVal{ + Value: "Org_0", + Type: 1, + } + srvItems["_change_selector"] = scv + + scv = &common.ColumnVal{ + Value: "tenant_id_xxxx", + Type: 1, + } + srvItems["tenant_id"] = scv + rows = append(rows, srvItems) + res := insertAPIproducts(rows, txn) + Expect(res).Should(BeTrue()) + } + + for i := 0; i < 10; i++ { + var rows []common.Row + srvItems := common.Row{} + result := strconv.FormatInt(int64(i), 10) + + scv := &common.ColumnVal{ + Value: "developer_id_" + result, + Type: 1, + } + srvItems["id"] = scv + + scv = &common.ColumnVal{ + Value: "test@apigee.com", + Type: 1, + } + srvItems["email"] = scv + + scv = &common.ColumnVal{ + Value: "Active", + Type: 1, + } + srvItems["status"] = scv + + scv = &common.ColumnVal{ + Value: "Apigee", + Type: 1, + } + srvItems["firstName"] = scv + + scv = &common.ColumnVal{ + Value: "Google", + Type: 1, + } + srvItems["lastName"] = scv + + scv = &common.ColumnVal{ + Value: "Org_0", + Type: 1, + } + srvItems["_change_selector"] = scv + + scv = &common.ColumnVal{ + Value: "tenant_id_xxxx", + Type: 1, + } + srvItems["tenant_id"] = scv + + rows = append(rows, srvItems) + res := insertDevelopers(rows, txn) + Expect(res).Should(BeTrue()) + } + + var j, k int + for i := 0; i < 10; i++ { + resulti := strconv.FormatInt(int64(i), 10) + for j = k; j < 10+k; j++ { + var rows []common.Row + + srvItems := common.Row{} + resultj := strconv.FormatInt(int64(j), 10) + + scv := &common.ColumnVal{ + Value: "application_id_" + resultj, + Type: 1, + } + srvItems["id"] = scv + + scv = &common.ColumnVal{ + Value: "developer_id_" + resulti, + Type: 1, + } + srvItems["developer_id"] = scv + + scv = &common.ColumnVal{ + Value: "approved", + Type: 1, + } + srvItems["status"] = scv + + scv = &common.ColumnVal{ + Value: "http://apigee.com", + Type: 1, + } + srvItems["callback_url"] = scv + + scv = &common.ColumnVal{ + Value: "Org_0", + Type: 1, + } + srvItems["_change_selector"] = scv + + scv = &common.ColumnVal{ + Value: "tenant_id_xxxx", + Type: 1, + } + srvItems["tenant_id"] = scv + rows = append(rows, srvItems) + res := insertApplications(rows, txn) + Expect(res).Should(BeTrue()) + } + k = j + } + + for i := 0; i < 10; i++ { + var rows []common.Row + srvItems := common.Row{} + result := strconv.FormatInt(int64(i), 10) + + scv := &common.ColumnVal{ + Value: "app_credential_" + result, + Type: 1, + } + srvItems["id"] = scv + + scv = &common.ColumnVal{ + Value: "application_id_" + result, + Type: 1, + } + srvItems["app_id"] = scv + + scv = &common.ColumnVal{ + Value: "approved", + Type: 1, + } + srvItems["status"] = scv + + scv = &common.ColumnVal{ + Value: "Org_0", + Type: 1, + } + srvItems["_change_selector"] = scv + + scv = &common.ColumnVal{ + Value: "tenant_id_xxxx", + Type: 1, + } + srvItems["tenant_id"] = scv + rows = append(rows, srvItems) + res := insertCredentials(rows, txn) + Expect(res).Should(BeTrue()) + } + + for i := 0; i < 10; i++ { + var rows []common.Row + srvItems := common.Row{} + result := strconv.FormatInt(int64(i), 10) + + scv := &common.ColumnVal{ + Value: "api_product_" + result, + Type: 1, + } + srvItems["apiprdt_id"] = scv + + scv = &common.ColumnVal{ + Value: "application_id_" + result, + Type: 1, + } + srvItems["app_id"] = scv + + scv = &common.ColumnVal{ + Value: "app_credential_" + result, + Type: 1, + } + srvItems["appcred_id"] = scv + scv = &common.ColumnVal{ + Value: "approved", + Type: 1, + } + srvItems["status"] = scv + scv = &common.ColumnVal{ + Value: "Org_0", + Type: 1, + } + srvItems["_change_selector"] = scv + + scv = &common.ColumnVal{ + Value: "tenant_id_xxxx", + Type: 1, + } + srvItems["tenant_id"] = scv + rows = append(rows, srvItems) + res := insertAPIProductMappers(rows, txn) + Expect(res).Should(BeTrue()) + } + + txn.Commit() + var count int64 + db.QueryRow("select count(*) from data_scope").Scan(&count) + log.Info("Found ", count) + + }) It("should reject a bad key", func() { v := url.Values{ - "key": []string{"credential_x"}, - "uriPath": []string{"/test"}, + "key": []string{"credential_x"}, + "uriPath": []string{"/test"}, "scopeuuid": []string{"ABCDE"}, - "action": []string{"verify"}, + "action": []string{"verify"}, } rsp, err := verifyAPIKey(v) Expect(err).ShouldNot(HaveOccurred()) @@ -31,38 +268,15 @@ Expect(respj.ErrInfo.ErrorCode).Should(Equal("REQ_ENTRY_NOT_FOUND")) }) - /* - It("should reject a key once it's deleted", func() { - pd0 := &DataPayload{ - EntityIdentifier: "app_credential_0", - } - res := deleteCredential(*pd0, db) - Expect(res).Should(BeTrue()) - var respj kmsResponseFail - v := url.Values{ - "key": []string{"app_credential_0"}, - "path": []string{"/test"}, - "env": []string{"Env_0"}, - "organization": []string{"Org_0"}, - "action": []string{"verify"}, - } - rsp, err := verifyAPIKey(v) - Expect(err).ShouldNot(HaveOccurred()) - - json.Unmarshal(rsp, &respj) - Expect(respj.Type).Should(Equal("ErrorResult")) - Expect(respj.ErrInfo.ErrorCode).Should(Equal("REQ_ENTRY_NOT_FOUND")) - }) - */ It("should successfully verify good keys", func() { for i := 1; i < 10; i++ { resulti := strconv.FormatInt(int64(i), 10) v := url.Values{ - "key": []string{"app_credential_"+resulti}, - "uriPath": []string{"/test"}, + "key": []string{"app_credential_" + resulti}, + "uriPath": []string{"/test"}, "scopeuuid": []string{"ABCDE"}, - "action": []string{"verify"}, + "action": []string{"verify"}, } rsp, err := verifyAPIKey(v) Expect(err).ShouldNot(HaveOccurred()) @@ -73,9 +287,231 @@ Expect(respj.RspInfo.Key).Should(Equal("app_credential_" + resulti)) } }) - }) - Context("access via API", func() { + It("Positive DB test for Delete operations", func() { + db := getDB() + txn, err := db.Begin() + Expect(err).ShouldNot(HaveOccurred()) + + for i := 0; i < 10; i++ { + srvItems := common.Row{} + result := strconv.FormatInt(int64(i), 10) + + scv := &common.ColumnVal{ + Value: "api_product_" + result, + Type: 3, + } + srvItems["apiprdt_id"] = scv + + scv = &common.ColumnVal{ + Value: "application_id_" + result, + Type: 3, + } + srvItems["app_id"] = scv + + scv = &common.ColumnVal{ + Value: "app_credential_" + result, + Type: 3, + } + srvItems["appcred_id"] = scv + + scv = &common.ColumnVal{ + Value: "Org_0", + Type: 3, + } + srvItems["_change_selector"] = scv + + res := deleteAPIproductMapper(srvItems, txn) + Expect(res).Should(BeTrue()) + } + + for i := 0; i < 10; i++ { + srvItems := common.Row{} + result := strconv.FormatInt(int64(i), 10) + + scv := &common.ColumnVal{ + Value: "app_credential_" + result, + Type: 3, + } + srvItems["id"] = scv + + scv = &common.ColumnVal{ + Value: "Org_0", + Type: 3, + } + srvItems["_change_selector"] = scv + + res := deleteObject("APP_CREDENTIAL", srvItems, txn) + Expect(res).Should(BeTrue()) + } + for i := 0; i < 100; i++ { + + srvItems := common.Row{} + resultj := strconv.FormatInt(int64(i), 10) + + scv := &common.ColumnVal{ + Value: "application_id_" + resultj, + Type: 1, + } + srvItems["id"] = scv + + scv = &common.ColumnVal{ + Value: "Org_0", + Type: 1, + } + srvItems["_change_selector"] = scv + + res := deleteObject("APP", srvItems, txn) + Expect(res).Should(BeTrue()) + } + + for i := 0; i < 10; i++ { + srvItems := common.Row{} + result := strconv.FormatInt(int64(i), 10) + + scv := &common.ColumnVal{ + Value: "developer_id_" + result, + Type: 1, + } + srvItems["id"] = scv + + scv = &common.ColumnVal{ + Value: "Org_0", + Type: 1, + } + srvItems["_change_selector"] = scv + + res := deleteObject("DEVELOPER", srvItems, txn) + Expect(res).Should(BeTrue()) + } + + for i := 0; i < 10; i++ { + srvItems := common.Row{} + result := strconv.FormatInt(int64(i), 10) + + scv := &common.ColumnVal{ + Value: "api_product_" + result, + Type: 1, + } + srvItems["id"] = scv + + scv = &common.ColumnVal{ + Value: "Org_0", + Type: 1, + } + srvItems["_change_selector"] = scv + + res := deleteObject("API_PRODUCT", srvItems, txn) + Expect(res).Should(BeTrue()) + } + + txn.Commit() + + }) + + It("Negative cases for DB Deletes on KMS tables", func() { + db := getDB() + txn, err := db.Begin() + Expect(err).ShouldNot(HaveOccurred()) + + srvItems := common.Row{} + result := "DEADBEEF" + + scv := &common.ColumnVal{ + Value: "api_product_" + result, + Type: 3, + } + srvItems["apiprdt_id"] = scv + + scv = &common.ColumnVal{ + Value: "application_id_" + result, + Type: 3, + } + srvItems["app_id"] = scv + + scv = &common.ColumnVal{ + Value: "app_credential_" + result, + Type: 3, + } + srvItems["appcred_id"] = scv + + scv = &common.ColumnVal{ + Value: "Org_0", + Type: 3, + } + srvItems["_change_selector"] = scv + + res := deleteAPIproductMapper(srvItems, txn) + Expect(res).Should(BeFalse()) + + res = deleteObject("API_PRODUCT", srvItems, txn) + Expect(res).Should(BeFalse()) + + res = deleteObject("APP_CREDENTIAL", srvItems, txn) + Expect(res).Should(BeFalse()) + + res = deleteObject("DEVELOPER", srvItems, txn) + Expect(res).Should(BeFalse()) + + res = deleteObject("APP", srvItems, txn) + Expect(res).Should(BeFalse()) + + txn.Rollback() + + }) + It("Negative cases for DB Inserts/updates on KMS tables", func() { + + db := getDB() + txn, err := db.Begin() + Expect(err).ShouldNot(HaveOccurred()) + + var rows []common.Row + srvItems := common.Row{} + result := "NOPRODID_BADCASE" + scv := &common.ColumnVal{ + Value: "foobar_" + result, + Type: 1, + } + srvItems[result] = scv + + scv = &common.ColumnVal{ + Value: "{/**, /test}", + Type: 1, + } + srvItems["api_resources"] = scv + + scv = &common.ColumnVal{ + Value: "{Env_1, Env_0}", + Type: 1, + } + srvItems["environments"] = scv + + scv = &common.ColumnVal{ + Value: "Org_0", + Type: 1, + } + srvItems["_change_selector"] = scv + + scv = &common.ColumnVal{ + Value: "tenant_id_xxxx", + Type: 1, + } + srvItems["tenant_id"] = scv + + rows = append(rows, srvItems) + res := insertAPIproducts(rows, txn) + Expect(res).Should(BeFalse()) + + res = insertApplications(rows, txn) + Expect(res).Should(BeFalse()) + + res = insertCredentials(rows, txn) + Expect(res).Should(BeFalse()) + + res = insertAPIProductMappers(rows, txn) + Expect(res).Should(BeFalse()) + + }) It("should reject a bad key", func() { @@ -103,33 +539,5 @@ Expect(respj.Type).Should(Equal("ErrorResult")) Expect(respj.ErrInfo.ErrorCode).Should(Equal("REQ_ENTRY_NOT_FOUND")) }) - - It("should successfully verify a good key", func() { - - uri, err := url.Parse(testServer.URL) - uri.Path = apiPath - - v := url.Values{} - v.Add("key", "app_credential_1") - v.Add("scopeuuid", "ABCDE") - v.Add("uriPath", "/test") - v.Add("action", "verify") - - client := &http.Client{} - req, err := http.NewRequest("POST", uri.String(), strings.NewReader(v.Encode())) - req.Header.Set("Content-Type", "application/x-www-form-urlencoded; param=value") - - res, err := client.Do(req) - defer res.Body.Close() - Expect(err).ShouldNot(HaveOccurred()) - - var respj kmsResponseSuccess - body, err := ioutil.ReadAll(res.Body) - Expect(err).ShouldNot(HaveOccurred()) - json.Unmarshal(body, &respj) - log.Info("Result test",respj) - Expect(respj.Type).Should(Equal("APIKeyContext")) - Expect(respj.RspInfo.Key).Should(Equal("app_credential_1")) - }) }) })
diff --git a/init.go b/init.go index a475420..98636b4 100644 --- a/init.go +++ b/init.go
@@ -10,9 +10,9 @@ ) var ( - log apid.LogService - data apid.DataService - events apid.EventsService + log apid.LogService + data apid.DataService + events apid.EventsService unsafeDB apid.DB dbMux sync.RWMutex ) @@ -160,7 +160,6 @@ } } - func createApidClusterTables(db apid.DB) { _, err := db.Exec(` CREATE TABLE apid_cluster ( @@ -196,4 +195,3 @@ log.Panic("Unable to initialize DB", err) } } -
diff --git a/listener.go b/listener.go index b2e3f34..de80a9b 100644 --- a/listener.go +++ b/listener.go
@@ -102,6 +102,12 @@ ele.Get("status", &status) ele.Get("issued_at", &issuedAt) ele.Get("tenant_id", &tenantId) + + /* Mandatory params check */ + if id == "" || scope == "" || tenantId == "" { + log.Error("INSERT APP_CREDENTIAL: i/p args missing") + return false + } _, err = txn.Stmt(prep).Exec( scope, id, @@ -152,6 +158,11 @@ ele.Get("updated_by", &LastModifiedBy) ele.Get("tenant_id", &tenantId) + /* Mandatory params check */ + if EntityIdentifier == "" || scope == "" || tenantId == "" { + log.Error("INSERT APP: i/p args missing") + return false + } _, err = txn.Stmt(prep).Exec( scope, EntityIdentifier, @@ -207,6 +218,11 @@ ele.Get("updated_at", &LastModifiedAt) ele.Get("updated_by", &LastModifiedBy) + /* Mandatory params check */ + if EntityIdentifier == "" || scope == "" || tenantId == "" { + log.Error("INSERT DEVELOPER: i/p args missing") + return false + } _, err = txn.Stmt(prep).Exec( scope, Email, @@ -253,6 +269,11 @@ ele.Get("environments", &env) ele.Get("tenant_id", &tenantId) + /* Mandatory params check */ + if apiProduct == "" || scope == "" || tenantId == "" { + log.Error("INSERT API_PRODUCT: i/p args missing") + return false + } _, err = txn.Stmt(prep).Exec( apiProduct, res, @@ -293,6 +314,12 @@ ele.Get("_change_selector", &Scope) ele.Get("status", &Status) + /* Mandatory params check */ + if ApiProduct == "" || AppId == "" || EntityIdentifier == "" || tenantId == "" || Scope == "" { + log.Error("INSERT APP_CREDENTIAL_APIPRODUCT_MAPPER : i/p args missing") + return false + } + /* * If the credentials has been successfully inserted, insert the * mapping entries associated with the credential @@ -437,7 +464,7 @@ */ func deleteObject(object string, ele common.Row, txn *sql.Tx) bool { - var scope, apiProduct string + var scope, objid string ssql := "DELETE FROM " + object + " WHERE id = $1 AND _change_selector = $2" prep, err := txn.Prepare(ssql) if err != nil { @@ -446,16 +473,18 @@ } defer prep.Close() ele.Get("_change_selector", &scope) - ele.Get("id", &apiProduct) + ele.Get("id", &objid) - _, err = txn.Stmt(prep).Exec(apiProduct, scope) - if err != nil { - log.Error("DELETE ", object, " Failed: (", apiProduct, ", ", scope, ")", err) - return false - } else { - log.Debug("DELETE ", object, " Success: (", apiProduct, ", ", scope, ")") - return true + res, err := txn.Stmt(prep).Exec(objid, scope) + if err == nil { + affect, err := res.RowsAffected() + if err == nil && affect != 0 { + log.Debugf("DELETE %s (%s, %s) success.", object, objid, scope) + return true + } } + log.Errorf("DELETE %s (%s, %s) failed.", object, objid, scope) + return false } @@ -478,23 +507,14 @@ ele.Get("appcred_id", &EntityIdentifier) ele.Get("_change_selector", &apid_scope) - _, err = txn.Stmt(prep).Exec(ApiProduct, AppId, EntityIdentifier, apid_scope) - if err != nil { - log.Error("DELETE APP_CREDENTIAL_APIPRODUCT_MAPPER Failed: (", - ApiProduct, ", ", - AppId, ", ", - EntityIdentifier, ", ", - apid_scope, - ")", - err) - return false - } else { - log.Debug("DELETE APP_CREDENTIAL_APIPRODUCT_MAPPER Success: (", - ApiProduct, ", ", - AppId, ", ", - EntityIdentifier, ", ", - apid_scope, - ")") - return true + res, err := txn.Stmt(prep).Exec(ApiProduct, AppId, EntityIdentifier, apid_scope) + if err == nil { + affect, err := res.RowsAffected() + if err == nil && affect != 0 { + log.Debugf("DELETE APP_CREDENTIAL_APIPRODUCT_MAPPER (%s, %s, %s, %s) success.", ApiProduct, AppId, EntityIdentifier, apid_scope) + return true + } } + log.Errorf("DELETE APP_CREDENTIAL_APIPRODUCT_MAPPER (%s, %s, %s, %s) failed.", ApiProduct, AppId, EntityIdentifier, apid_scope, err) + return false }
diff --git a/listener_test.go b/listener_test.go index a8da554..8305836 100644 --- a/listener_test.go +++ b/listener_test.go
@@ -11,151 +11,387 @@ var _ = Describe("listener", func() { - It("should store data from ApigeeSync in the database", func(done Done) { + Context("KMS create/updates verification via changes", func() { + It("Create KMS tables via changes, and Verify via verifyApiKey", func(done Done) { + var event = common.ChangeList{} + closed := 0 + /* API Product */ + srvItems := common.Row{ + "id": { + Value: "ch_api_product_2", + }, + "apid_resources": { + Value: "{}", + }, + "environments": { + Value: "{Env_0, Env_1}", + }, + "tenant_id": { + Value: "tenant_id_0", + }, + "_change_selector": { + Value: "test_org0", + }, + } + /* DEVELOPER */ + devItems := common.Row{ + "id": { + Value: "ch_developer_id_2", + }, + "status": { + Value: "Active", + }, + "tenant_id": { + Value: "tenant_id_0", + }, + "_change_selector": { + Value: "test_org0", + }, + } + + /* APP */ + appItems := common.Row{ + "id": { + Value: "ch_application_id_2", + }, + "developer_id": { + Value: "ch_developer_id_2", + }, + "status": { + Value: "Approved", + }, + "tenant_id": { + Value: "tenant_id_0", + }, + "_change_selector": { + Value: "test_org0", + }, + } + + /* CRED */ + credItems := common.Row{ + "id": { + Value: "ch_app_credential_2", + }, + "app_id": { + Value: "ch_application_id_2", + }, + "tenant_id": { + Value: "tenant_id_0", + }, + "status": { + Value: "Approved", + }, + "_change_selector": { + Value: "test_org0", + }, + } + + /* APP_CRED_APIPRD_MAPPER */ + mpItems := common.Row{ + "apiprdt_id": { + Value: "ch_api_product_2", + }, + "app_id": { + Value: "ch_application_id_2", + }, + "appcred_id": { + Value: "ch_app_credential_2", + }, + "status": { + Value: "Approved", + }, + "_change_selector": { + Value: "test_org0", + }, + "tenant_id": { + Value: "tenant_id_0", + }, + } + + event.Changes = []common.Change{ + { + Table: "kms.api_product", + NewRow: srvItems, + Operation: 1, + }, + { + Table: "kms.developer", + NewRow: devItems, + Operation: 1, + }, + { + Table: "kms.app", + NewRow: appItems, + Operation: 1, + }, + { + Table: "kms.app_credential", + NewRow: credItems, + Operation: 1, + }, + { + Table: "kms.app_credential_apiproduct_mapper", + NewRow: mpItems, + Operation: 1, + }, + } + + h := &test_handler{ + "checkDatabase post Insertion", + func(e apid.Event) { + defer GinkgoRecover() + + // ignore the first event, let standard listener process it + changeSet := e.(*common.ChangeList) + if len(changeSet.Changes) > 0 || closed == 1 { + return + } + v := url.Values{ + "key": []string{"ch_app_credential_2"}, + "uriPath": []string{"/test"}, + "scopeuuid": []string{"XYZ"}, + "action": []string{"verify"}, + } + rsp, err := verifyAPIKey(v) + Expect(err).ShouldNot(HaveOccurred()) + var respj kmsResponseSuccess + json.Unmarshal(rsp, &respj) + Expect(respj.Type).Should(Equal("APIKeyContext")) + Expect(respj.RspInfo.Key).Should(Equal("ch_app_credential_2")) + closed = 1 + close(done) + }, + } + + apid.Events().Listen("ApigeeSync", h) + apid.Events().Emit("ApigeeSync", &event) + apid.Events().Emit("ApigeeSync", &common.ChangeList{}) + }) + }) + + It("Modify tables in KMS tables, and verify via verifyApiKey", func(done Done) { + closed := 0 var event = common.ChangeList{} var event2 = common.ChangeList{} + /* Orig data */ /* API Product */ - srvItems := common.Row{ + srvItemsOld := common.Row{ "id": { Value: "ch_api_product_0", - Type: 1, }, "apid_resources": { Value: "{}", - Type: 1, }, "environments": { Value: "{Env_0, Env_1}", - Type: 1, }, "tenant_id": { Value: "tenant_id_0", - Type: 1, }, "_change_selector": { Value: "test_org0", - Type: 1, }, } /* DEVELOPER */ - devItems := common.Row{ + devItemsOld := common.Row{ "id": { Value: "ch_developer_id_0", - Type: 1, }, "status": { Value: "Active", - Type: 1, }, "tenant_id": { Value: "tenant_id_0", - Type: 1, }, "_change_selector": { Value: "test_org0", - Type: 1, }, } /* APP */ - appItems := common.Row{ + appItemsOld := common.Row{ "id": { Value: "ch_application_id_0", - Type: 1, }, "developer_id": { Value: "ch_developer_id_0", - Type: 1, }, "status": { Value: "Approved", - Type: 1, }, "tenant_id": { Value: "tenant_id_0", - Type: 1, }, "_change_selector": { Value: "test_org0", - Type: 1, }, } /* CRED */ - credItems := common.Row{ + credItemsOld := common.Row{ "id": { Value: "ch_app_credential_0", - Type: 1, }, "app_id": { Value: "ch_application_id_0", - Type: 1, }, "tenant_id": { Value: "tenant_id_0", - Type: 1, }, "status": { Value: "Approved", - Type: 1, }, "_change_selector": { Value: "test_org0", - Type: 1, }, } /* APP_CRED_APIPRD_MAPPER */ - mpItems := common.Row{ + mpItemsOld := common.Row{ "apiprdt_id": { Value: "ch_api_product_0", - Type: 1, }, "app_id": { Value: "ch_application_id_0", - Type: 1, }, "appcred_id": { Value: "ch_app_credential_0", - Type: 1, }, "status": { Value: "Approved", - Type: 1, }, "_change_selector": { Value: "test_org0", - Type: 1, + }, + "tenant_id": { + Value: "tenant_id_0", + }, + } + + /* New to be replaced data */ + /* API PRODUCT */ + srvItemsNew := common.Row{ + "id": { + Value: "ch_api_product_1", + }, + "apid_resources": { + Value: "{}", + }, + "environments": { + Value: "{Env_0, Env_1}", + }, + "tenant_id": { + Value: "tenant_id_0", + }, + "_change_selector": { + Value: "test_org0", + }, + } + + /* DEVELOPER */ + devItemsNew := common.Row{ + "id": { + Value: "ch_developer_id_1", + }, + "status": { + Value: "Active", + }, + "tenant_id": { + Value: "tenant_id_0", + }, + "_change_selector": { + Value: "test_org0", + }, + } + + /* APP */ + appItemsNew := common.Row{ + "id": { + Value: "ch_application_id_1", + }, + "developer_id": { + Value: "ch_developer_id_1", + }, + "status": { + Value: "Approved", + }, + "tenant_id": { + Value: "tenant_id_0", + }, + "_change_selector": { + Value: "test_org0", + }, + } + + /* CRED */ + credItemsNew := common.Row{ + "id": { + Value: "ch_app_credential_1", + }, + "app_id": { + Value: "ch_application_id_1", + }, + "tenant_id": { + Value: "tenant_id_0", + }, + "status": { + Value: "Approved", + }, + "_change_selector": { + Value: "test_org0", + }, + } + + /* APP_CRED_APIPRD_MAPPER */ + mpItemsNew := common.Row{ + "apiprdt_id": { + Value: "ch_api_product_1", + }, + "app_id": { + Value: "ch_application_id_1", + }, + "appcred_id": { + Value: "ch_app_credential_1", + }, + "status": { + Value: "Approved", + }, + "_change_selector": { + Value: "test_org0", + }, + "tenant_id": { + Value: "tenant_id_0", }, } event.Changes = []common.Change{ { Table: "kms.api_product", - NewRow: srvItems, + NewRow: srvItemsOld, Operation: 1, }, { Table: "kms.developer", - NewRow: devItems, + NewRow: devItemsOld, Operation: 1, }, { Table: "kms.app", - NewRow: appItems, + NewRow: appItemsOld, Operation: 1, }, { Table: "kms.app_credential", - NewRow: credItems, + NewRow: credItemsOld, Operation: 1, }, { Table: "kms.app_credential_apiproduct_mapper", - NewRow: mpItems, + NewRow: mpItemsOld, Operation: 1, }, } @@ -163,30 +399,36 @@ event2.Changes = []common.Change{ { Table: "kms.api_product", - OldRow: srvItems, - Operation: 3, + OldRow: srvItemsOld, + NewRow: srvItemsNew, + Operation: 2, }, { Table: "kms.developer", - OldRow: devItems, - Operation: 3, + OldRow: devItemsOld, + NewRow: devItemsNew, + Operation: 2, }, { Table: "kms.app", - OldRow: appItems, - Operation: 3, + OldRow: appItemsOld, + NewRow: appItemsNew, + Operation: 2, }, { Table: "kms.app_credential", - OldRow: credItems, - Operation: 3, + OldRow: credItemsOld, + NewRow: credItemsNew, + Operation: 2, }, { Table: "kms.app_credential_apiproduct_mapper", - OldRow: mpItems, - Operation: 3, + OldRow: mpItemsOld, + NewRow: mpItemsNew, + Operation: 2, }, } + h := &test_handler{ "checkDatabase post Insertion", func(e apid.Event) { @@ -194,21 +436,22 @@ // ignore the first event, let standard listener process it changeSet := e.(*common.ChangeList) - if len(changeSet.Changes) > 0 { + if len(changeSet.Changes) > 0 || closed == 1 { return } v := url.Values{ - "key": []string{"ch_app_credential_0"}, - "uriPath": []string{"/test"}, + "key": []string{"ch_app_credential_1"}, + "uriPath": []string{"/test"}, "scopeuuid": []string{"XYZ"}, - "action": []string{"verify"}, + "action": []string{"verify"}, } rsp, err := verifyAPIKey(v) Expect(err).ShouldNot(HaveOccurred()) var respj kmsResponseSuccess json.Unmarshal(rsp, &respj) Expect(respj.Type).Should(Equal("APIKeyContext")) - Expect(respj.RspInfo.Key).Should(Equal("ch_app_credential_0")) + Expect(respj.RspInfo.Key).Should(Equal("ch_app_credential_1")) + closed = 1 close(done) }, } @@ -216,7 +459,6 @@ apid.Events().Listen("ApigeeSync", h) apid.Events().Emit("ApigeeSync", &event) apid.Events().Emit("ApigeeSync", &event2) - apid.Events().Emit("ApigeeSync", &event) apid.Events().Emit("ApigeeSync", &common.ChangeList{}) }) @@ -234,3 +476,29 @@ func (t *test_handler) Handle(event apid.Event) { t.f(event) } + +func addScopes(db apid.DB) { + txn, _ := db.Begin() + txn.Exec("INSERT INTO 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 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 DATA_SCOPE for test") + txn.Commit() +}
diff --git a/verifyAPIKey_suite_test.go b/verifyAPIKey_suite_test.go index 870a376..a5e5b39 100644 --- a/verifyAPIKey_suite_test.go +++ b/verifyAPIKey_suite_test.go
@@ -4,15 +4,13 @@ . "github.com/onsi/ginkgo" . "github.com/onsi/gomega" - "testing" "github.com/30x/apid" "github.com/30x/apid/factory" "io/ioutil" - "net/http/httptest" "net/http" + "net/http/httptest" "os" - "strconv" - "github.com/apigee-labs/transicator/common" + "testing" ) var ( @@ -38,7 +36,7 @@ setDB(db) createTables(db) createApidClusterTables(db) - insertTestData(db) + addScopes(db) testServer = httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) { if req.URL.Path == apiPath { handleRequest(w, req) @@ -58,259 +56,3 @@ RegisterFailHandler(Fail) RunSpecs(t, "VerifyAPIKey Suite") } - -func insertTestData(db apid.DB) { - - txn, err := db.Begin() - Expect(err).ShouldNot(HaveOccurred()) - - for i := 0; i < 10; i++ { - var rows []common.Row - srvItems := common.Row{} - result := strconv.FormatInt(int64(i), 10) - - scv := &common.ColumnVal{ - Value: "api_product_" + result, - Type: 1, - } - srvItems["id"] = scv - - scv = &common.ColumnVal{ - Value: "{/**, /test}", - Type: 1, - } - srvItems["api_resources"] = scv - - scv = &common.ColumnVal{ - Value: "{Env_0, Env_1}", - Type: 1, - } - srvItems["environments"] = scv - - scv = &common.ColumnVal{ - Value: "Org_0", - Type: 1, - } - srvItems["_change_selector"] = scv - - scv = &common.ColumnVal{ - Value: "tenant_id_xxxx", - Type: 1, - } - srvItems["tenant_id"] = scv - rows = append(rows, srvItems) - res := insertAPIproducts(rows, txn) - Expect(res).Should(BeTrue()) - } - - for i := 0; i < 10; i++ { - var rows []common.Row - srvItems := common.Row{} - result := strconv.FormatInt(int64(i), 10) - - scv := &common.ColumnVal{ - Value: "developer_id_" + result, - Type: 1, - } - srvItems["id"] = scv - - scv = &common.ColumnVal{ - Value: "test@apigee.com", - Type: 1, - } - srvItems["email"] = scv - - scv = &common.ColumnVal{ - Value: "Active", - Type: 1, - } - srvItems["status"] = scv - - scv = &common.ColumnVal{ - Value: "Apigee", - Type: 1, - } - srvItems["firstName"] = scv - - scv = &common.ColumnVal{ - Value: "Google", - Type: 1, - } - srvItems["lastName"] = scv - - scv = &common.ColumnVal{ - Value: "Org_0", - Type: 1, - } - srvItems["_change_selector"] = scv - - scv = &common.ColumnVal{ - Value: "tenant_id_xxxx", - Type: 1, - } - srvItems["tenant_id"] = scv - - rows = append(rows, srvItems) - res := insertDevelopers(rows, txn) - Expect(res).Should(BeTrue()) - } - - var j, k int - for i := 0; i < 10; i++ { - resulti := strconv.FormatInt(int64(i), 10) - for j = k; j < 10 + k; j++ { - var rows []common.Row - - srvItems := common.Row{} - resultj := strconv.FormatInt(int64(j), 10) - - scv := &common.ColumnVal{ - Value: "application_id_" + resultj, - Type: 1, - } - srvItems["id"] = scv - - scv = &common.ColumnVal{ - Value: "developer_id_" + resulti, - Type: 1, - } - srvItems["developer_id"] = scv - - scv = &common.ColumnVal{ - Value: "approved", - Type: 1, - } - srvItems["status"] = scv - - scv = &common.ColumnVal{ - Value: "http://apigee.com", - Type: 1, - } - srvItems["callback_url"] = scv - - scv = &common.ColumnVal{ - Value: "Org_0", - Type: 1, - } - srvItems["_change_selector"] = scv - - scv = &common.ColumnVal{ - Value: "tenant_id_xxxx", - Type: 1, - } - srvItems["tenant_id"] = scv - rows = append(rows, srvItems) - res := insertApplications(rows, txn) - Expect(res).Should(BeTrue()) - } - k = j - } - - for i := 0; i < 10; i++ { - var rows []common.Row - srvItems := common.Row{} - result := strconv.FormatInt(int64(i), 10) - - scv := &common.ColumnVal{ - Value: "app_credential_" + result, - Type: 1, - } - srvItems["id"] = scv - - scv = &common.ColumnVal{ - Value: "application_id_" + result, - Type: 1, - } - srvItems["app_id"] = scv - - scv = &common.ColumnVal{ - Value: "approved", - Type: 1, - } - srvItems["status"] = scv - - scv = &common.ColumnVal{ - Value: "Org_0", - Type: 1, - } - srvItems["_change_selector"] = scv - - scv = &common.ColumnVal{ - Value: "tenant_id_xxxx", - Type: 1, - } - srvItems["tenant_id"] = scv - rows = append(rows, srvItems) - res := insertCredentials(rows, txn) - Expect(res).Should(BeTrue()) - } - - for i := 0; i < 10; i++ { - var rows []common.Row - srvItems := common.Row{} - result := strconv.FormatInt(int64(i), 10) - - scv := &common.ColumnVal{ - Value: "api_product_" + result, - Type: 1, - } - srvItems["apiprdt_id"] = scv - - scv = &common.ColumnVal{ - Value: "application_id_" + result, - Type: 1, - } - srvItems["app_id"] = scv - - scv = &common.ColumnVal{ - Value: "app_credential_" + result, - Type: 1, - } - srvItems["appcred_id"] = scv - scv = &common.ColumnVal{ - Value: "approved", - Type: 1, - } - srvItems["status"] = scv - scv = &common.ColumnVal{ - Value: "Org_0", - Type: 1, - } - srvItems["_change_selector"] = scv - - scv = &common.ColumnVal{ - Value: "tenant_id_xxxx", - Type: 1, - } - srvItems["tenant_id"] = scv - rows = append(rows, srvItems) - res := insertAPIProductMappers(rows, txn) - Expect(res).Should(BeTrue()) - } - txn.Exec("INSERT INTO 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 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 DATA_SCOPE for test") - txn.Commit() - var count int64 - db.QueryRow("select count(*) from data_scope").Scan(&count); - log.Info("Found ", count) - -}