ocket8888 commented on a change in pull request #4374: Create API v2 tests URL: https://github.com/apache/trafficcontrol/pull/4374#discussion_r375404378
########## File path: traffic_ops/testing/api/v2/roles_test.go ########## @@ -0,0 +1,182 @@ +package v2 + +/* + + 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. +*/ + +import ( + "net/http" + "reflect" + "testing" + + "github.com/apache/trafficcontrol/lib/go-log" + "github.com/apache/trafficcontrol/lib/go-tc" +) + +const ( + roleGood = 0 + roleInvalidCap = 1 + roleNeedCap = 2 + roleBadPrivLevel = 3 +) + +func TestRoles(t *testing.T) { + WithObjs(t, []TCObj{Roles}, func() { + UpdateTestRoles(t) + GetTestRoles(t) + GetTestRolesV11(t) + VerifyGetRolesOrder(t) + }) +} + +func CreateTestRoles(t *testing.T) { + expectedAlerts := []tc.Alerts{tc.Alerts{[]tc.Alert{tc.Alert{"role was created.", "success"}}}, tc.Alerts{[]tc.Alert{tc.Alert{"can not add non-existent capabilities: [invalid-capability]", "error"}}}, tc.Alerts{[]tc.Alert{tc.Alert{"role was created.", "success"}}}} + for i, role := range testData.Roles { + var alerts tc.Alerts + alerts, _, status, err := TOSession.CreateRole(role) + log.Debugln("Status Code: ", status) + log.Debugln("Response: ", alerts) + if err != nil { + log.Debugf("error: %v", err) + //t.Errorf("could not CREATE role: %v", err) + } + if !reflect.DeepEqual(alerts, expectedAlerts[i]) { + t.Errorf("got alerts: %v but expected alerts: %v", alerts, expectedAlerts[i]) + } + } +} + +func UpdateTestRoles(t *testing.T) { + log.Debugf("testData.Roles contains: %+v\n", testData.Roles) + firstRole := testData.Roles[0] + // Retrieve the Role by role so we can get the id for the Update + resp, _, status, err := TOSession.GetRoleByName(*firstRole.Name) + log.Debugln("Status Code: ", status) + if err != nil { + t.Errorf("cannot GET Role by role: %v - %v", firstRole.Name, err) + } + log.Debugf("got response: %+v\n", resp) + remoteRole := resp[0] + expectedRole := "new admin2" + remoteRole.Name = &expectedRole + var alert tc.Alerts + alert, _, status, err = TOSession.UpdateRoleByID(*remoteRole.ID, remoteRole) + log.Debugln("Status Code: ", status) + if err != nil { + t.Errorf("cannot UPDATE Role by id: %v - %v", err, alert) + } + + // Retrieve the Role to check role got updated + resp, _, status, err = TOSession.GetRoleByID(*remoteRole.ID) + log.Debugln("Status Code: ", status) + if err != nil { + t.Errorf("cannot GET Role by role: %v - %v", firstRole.Name, err) + } + respRole := resp[0] + if *respRole.Name != expectedRole { + t.Errorf("results do not match actual: %s, expected: %s", *respRole.Name, expectedRole) + } + + // Set the name back to the fixture value so we can delete it after + remoteRole.Name = firstRole.Name + alert, _, status, err = TOSession.UpdateRoleByID(*remoteRole.ID, remoteRole) + log.Debugln("Status Code: ", status) + if err != nil { + t.Errorf("cannot UPDATE Role by id: %v - %v", err, alert) + } + +} + +func GetTestRoles(t *testing.T) { + role := testData.Roles[roleGood] + resp, _, status, err := TOSession.GetRoleByName(*role.Name) + log.Debugln("Status Code: ", status) + if err != nil { + t.Errorf("cannot GET Role by role: %v - %v", err, resp) + } + +} + +func GetTestRolesV11(t *testing.T) { Review comment: This whole test `func` serves no purpose in a test suite for API v2.x. ---------------------------------------------------------------- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. For queries about this service, please contact Infrastructure at: [email protected] With regards, Apache Git Services
