ocket8888 commented on a change in pull request #4374: Create API v2 tests URL: https://github.com/apache/trafficcontrol/pull/4374#discussion_r375408321
########## File path: traffic_ops/testing/api/v2/user_test.go ########## @@ -0,0 +1,437 @@ +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 ( + "bytes" + "fmt" + "net/http" + "strings" + "testing" + "time" + + "github.com/apache/trafficcontrol/lib/go-log" + "github.com/apache/trafficcontrol/lib/go-tc" + "github.com/apache/trafficcontrol/lib/go-util" + toclient "github.com/apache/trafficcontrol/traffic_ops/client" +) + +func TestUsers(t *testing.T) { + WithObjs(t, []TCObj{CDNs, Types, Tenants, Parameters, Profiles, Statuses, Divisions, Regions, PhysLocations, CacheGroups, DeliveryServices, Users}, func() { + UpdateTestUsers(t) + RolenameCapitalizationTest(t) + OpsUpdateAdminTest(t) + UserSelfUpdateTest(t) + UserUpdateOwnRoleTest(t) + GetTestUsers(t) + GetTestUserCurrent(t) + UserTenancyTest(t) + }) +} + +const SessionUserName = "admin" // TODO make dynamic? + +func CreateTestUsers(t *testing.T) { + for _, user := range testData.Users { + + resp, _, err := TOSession.CreateUser(&user) + if err != nil { + t.Errorf("could not CREATE user: %v", err) + } + log.Debugln("Response: ", resp.Alerts) + } +} + +func RolenameCapitalizationTest(t *testing.T) { + + roles, _, _, err := TOSession.GetRoles() + if err != nil { + t.Errorf("could not get roles: %v", err) + } + if len(roles) == 0 { + t.Fatal("there should be at least one role to test the user") + } + + tenants, _, err := TOSession.Tenants() + if err != nil { + t.Errorf("could not get tenants: %v", err) + } + if len(tenants) == 0 { + t.Fatal("there should be at least one tenant to test the user") + } + + // this user never does anything, so the role and tenant aren't important + blob := fmt.Sprintf(` + { + "username": "test_user", + "email": "[email protected]", + "fullName": "full name is required", + "localPasswd": "better_twelve", + "confirmLocalPasswd": "better_twelve", + "role": %d, + "tenantId": %d + }`, *roles[0].ID, tenants[0].ID) + + reader := strings.NewReader(blob) + request, err := http.NewRequest("POST", fmt.Sprintf("%v/api/1.4/users", TOSession.URL), reader) + if err != nil { + t.Errorf("could not make new request: %v", err) + } + resp, err := TOSession.Client.Do(request) + if err != nil { + t.Errorf("could not do request: %v", err) + } + + buf := new(bytes.Buffer) + buf.ReadFrom(resp.Body) + strResp := buf.String() + if !strings.Contains(strResp, "roleName") { + t.Error("incorrect json was returned for POST") + } + + request, err = http.NewRequest("GET", fmt.Sprintf("%v/api/1.4/users?username=test_user", TOSession.URL), nil) + resp, err = TOSession.Client.Do(request) + + buf = new(bytes.Buffer) + buf.ReadFrom(resp.Body) + strResp = buf.String() + if !strings.Contains(strResp, "rolename") { + t.Error("incorrect json was returned for GET") + } + +} + +func OpsUpdateAdminTest(t *testing.T) { + toReqTimeout := time.Second * time.Duration(Config.Default.Session.TimeoutInSecs) + opsTOClient, _, err := toclient.LoginWithAgent(TOSession.URL, "opsuser", "pa$$word", true, "to-api-v1-client-tests/opsuser", true, toReqTimeout) + if err != nil { + t.Fatalf("failed to get log in with opsuser: %v", err.Error()) + } + + resp, _, err := TOSession.GetUserByUsername("admin") + if err != nil { + t.Errorf("cannot GET user by name: 'admin', %v", err) + } + user := resp[0] + + fullName := "oops" + email := "[email protected]" + user.FullName = &fullName + user.Email = &email + + _, _, err = opsTOClient.UpdateUserByID(*user.ID, &user) + if err == nil { + t.Error("ops user incorrectly updated an admin") + } +} + +func UserSelfUpdateTest(t *testing.T) { + toReqTimeout := time.Second * time.Duration(Config.Default.Session.TimeoutInSecs) + opsTOClient, _, err := toclient.LoginWithAgent(TOSession.URL, "opsuser", "pa$$word", true, "to-api-v1-client-tests/opsuser", true, toReqTimeout) + if err != nil { + t.Fatalf("failed to get log in with opsuser: %v", err.Error()) + } + + resp, _, err := TOSession.GetUserByUsername("opsuser") + if err != nil { + t.Fatalf("cannot GET user by name: 'opsuser', %v\n", err) + } + if len(resp) < 1 { + t.Fatalf("no users returned when requesting user 'opsuser'") + } + user := resp[0] + + if user.ID == nil { + t.Fatalf("user 'opsuser' has a null or missing ID - cannot proceed") + } + + user.FullName = util.StrPtr("Oops-man") + user.Email = util.StrPtr("[email protected]") + + var updateResp *tc.UpdateUserResponse + updateResp, _, err = opsTOClient.UpdateUserByID(*user.ID, &user) + if err != nil { + t.Fatalf("cannot UPDATE user by id: %v - %v\n", err, updateResp) + } + + // Make sure it got updated + resp2, _, err := TOSession.GetUserByID(*user.ID) + if err != nil { + t.Fatalf("cannot GET user by id: '%d', %v\n", *user.ID, err) + } + if len(resp2) < 1 { + t.Fatalf("no results returned when requesting user #%d", *user.ID) + } + updatedUser := resp2[0] + + if updatedUser.FullName == nil { + t.Errorf("user was not correctly updated, FullName is null or missing") + } else if *updatedUser.FullName != "Oops-man" { + t.Errorf("results do not match actual: '%s', expected: 'Oops-man'\n", *updatedUser.FullName) + } + + if updatedUser.Email == nil { + t.Errorf("user was not correctly updated, Email is null or missing") + } else if *updatedUser.Email != "[email protected]" { + t.Errorf("results do not match actual: '%s', expected: '[email protected]'\n", *updatedUser.Email) + } + + + // Same thing using /user/current + user.FullName = util.StrPtr("ops-man") + user.Email = util.StrPtr("[email protected]") + updateResp, _, err = opsTOClient.UpdateCurrentUser(user) + if err != nil { + t.Fatalf("error updating current user: %v - %v", err, updateResp) + } + + // Make sure it got updated + resp2, _, err = TOSession.GetUserByID(*user.ID) + if err != nil { + t.Fatalf("error getting user #%d: %v", *user.ID, err) + } + + if len(resp2) < 1 { + t.Fatalf("no user returned when requesting user #%d", *user.ID) + } + + if resp2[0].FullName == nil { + t.Errorf("FullName missing or null after update") + } else if *resp2[0].FullName != "ops-man" { + t.Errorf("Expected FullName to be 'ops-man', but it was '%s'", *resp2[0].FullName) + } + + if resp2[0].Email == nil { + t.Errorf("Email missing or null after update") + } else if *resp2[0].Email != "[email protected]" { + t.Errorf("Expected Email to be restored to '[email protected]', but it was '%s'", *resp2[0].Email) + } + + // now test using an invalid email address + currentEmail := *user.Email + user.Email = new(string); + updateResp, _, err = TOSession.UpdateCurrentUser(user) + if err == nil { + t.Fatal("error was expected updating user with email: '' - got none") + } + + // Ensure it wasn't actually updated + resp2, _, err = TOSession.GetUserByID(*user.ID) + if err != nil { + t.Fatalf("error getting user #%d: %v", *user.ID, err) + } + + if len(resp2) < 1 { + t.Fatalf("no user returned when requesting user #%d", *user.ID) + } + + if resp2[0].Email == nil { + t.Errorf("Email missing or null after update") + } else if *resp2[0].Email != currentEmail { + t.Errorf("Expected Email to still be '%s', but it was '%s'", currentEmail, *resp2[0].Email) + } +} + +func UserUpdateOwnRoleTest(t *testing.T) { + resp, _, err := TOSession.GetUserByUsername(SessionUserName) + if err != nil { + t.Errorf("cannot GET user by name: '%s', %v", SessionUserName, err) + } + user := resp[0] + + *user.Role = *user.Role + 1 + _, _, err = TOSession.UpdateUserByID(*user.ID, &user) + if err == nil { + t.Error("user incorrectly updated their role") + } +} + +func UpdateTestUsers(t *testing.T) { + firstUsername := *testData.Users[0].Username + resp, _, err := TOSession.GetUserByUsername(firstUsername) + if err != nil { + t.Errorf("cannot GET user by name: '%s', %v", firstUsername, err) + } + user := resp[0] + newCity := "kidz kable kown" + *user.City = newCity + + var updateResp *tc.UpdateUserResponse + updateResp, _, err = TOSession.UpdateUserByID(*user.ID, &user) + if err != nil { + t.Errorf("cannot UPDATE user by id: %v - %v", err, updateResp.Alerts) + } + + // Make sure it got updated + resp2, _, err := TOSession.GetUserByID(*user.ID) + if err != nil { + t.Errorf("cannot GET user by id: '%d', %v", *user.ID, err) + } + updatedUser := resp2[0] + if *updatedUser.City != newCity { + t.Errorf("results do not match actual: %s, expected: %s", *updatedUser.City, newCity) + } +} + +func GetTestUsers(t *testing.T) { + _, _, err := TOSession.GetUsers() + if err != nil { + t.Errorf("cannot GET users: %v", err) + } +} + +func GetTestUserCurrent(t *testing.T) { + user, _, err := TOSession.GetUserCurrent() + if err != nil { + t.Errorf("cannot GET current user: %v", err) + } + if user.UserName == nil { + t.Errorf("current user expected: %v actual: %v", SessionUserName, nil) + } + if *user.UserName != SessionUserName { + t.Errorf("current user expected: %v actual: %v", SessionUserName, *user.UserName) + } +} + +func UserTenancyTest(t *testing.T) { + users, _, err := TOSession.GetUsers() + if err != nil { + t.Errorf("cannot GET users: %v", err) + } + tenant3Found := false + tenant4Found := false + tenant3Username := "tenant3user" + tenant4Username := "tenant4user" + tenant3User := tc.User{} + + // assert admin user can view tenant3user and tenant4user + for _, user := range users { + if *user.Username == tenant3Username { + tenant3Found = true + tenant3User = user + } else if *user.Username == tenant4Username { + tenant4Found = true + } + if tenant3Found && tenant4Found { + break + } + } + if !tenant3Found || !tenant4Found { + t.Error("expected admin to be able to view tenants: tenant3 and tenant4") + } + + toReqTimeout := time.Second * time.Duration(Config.Default.Session.TimeoutInSecs) + tenant4TOClient, _, err := toclient.LoginWithAgent(TOSession.URL, "tenant4user", "pa$$word", true, "to-api-v1-client-tests/tenant4user", true, toReqTimeout) Review comment: should say v2 now ---------------------------------------------------------------- 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
