ocket8888 commented on a change in pull request #4374: Create API v2 tests
URL: https://github.com/apache/trafficcontrol/pull/4374#discussion_r375407035
 
 

 ##########
 File path: traffic_ops/testing/api/v2/tenants_test.go
 ##########
 @@ -0,0 +1,306 @@
+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 (
+       "strconv"
+       "strings"
+       "testing"
+       "time"
+
+       "github.com/apache/trafficcontrol/lib/go-tc"
+       "github.com/apache/trafficcontrol/traffic_ops/client"
+)
+
+func TestTenants(t *testing.T) {
+       WithObjs(t, []TCObj{Parameters, Tenants}, func() {
+               GetTestTenants(t)
+               UpdateTestTenants(t)
+       })
+}
+
+func CreateTestTenants(t *testing.T) {
+       for _, ten := range testData.Tenants {
+               resp, err := TOSession.CreateTenant(&ten)
+
+               if err != nil {
+                       t.Errorf("could not CREATE tenant %s: %v", ten.Name, 
err)
+               }
+               if resp.Response.Name != ten.Name {
+                       t.Errorf("expected tenant %+v; got %+v", ten, 
resp.Response)
+               }
+       }
+}
+
+func GetTestTenants(t *testing.T) {
+       resp, _, err := TOSession.Tenants()
+       if err != nil {
+               t.Errorf("cannot GET all tenants: %v - %v", err, resp)
+               return
+       }
+       foundTenants := make(map[string]tc.Tenant, len(resp))
+       for _, ten := range resp {
+               foundTenants[ten.Name] = ten
+       }
+
+       // expect root and badTenant (defined in todb.go) + all defined in 
testData.Tenants
+       if len(resp) != 2+len(testData.Tenants) {
+               t.Errorf("expected %d tenants,  got %d", 
2+len(testData.Tenants), len(resp))
+       }
+
+       for _, ten := range testData.Tenants {
+               if ft, ok := foundTenants[ten.Name]; ok {
+                       if ft.ParentName != ten.ParentName {
+                               t.Errorf("tenant %s: expected parent %s,  got 
%s", ten.Name, ten.ParentName, ft.ParentName)
+                       }
+               } else {
+                       t.Errorf("expected tenant %s: not found", ten.Name)
+               }
+       }
+}
+
+func UpdateTestTenants(t *testing.T) {
+
+       // Retrieve the Tenant by name so we can get the id for the Update
+       name := "tenant2"
+       parentName := "tenant1"
+       modTenant, _, err := TOSession.TenantByName(name)
+       if err != nil {
+               t.Errorf("cannot GET Tenant by name: %s - %v", name, err)
+       }
+
+       newParent, _, err := TOSession.TenantByName(parentName)
+       if err != nil {
+               t.Errorf("cannot GET Tenant by name: %s - %v", parentName, err)
+       }
+       modTenant.ParentID = newParent.ID
+
+       _, err = TOSession.UpdateTenant(strconv.Itoa(modTenant.ID), modTenant)
+       if err != nil {
+               t.Errorf("cannot UPDATE Tenant by id: %v", err)
+       }
+
+       // Retrieve the Tenant to check Tenant parent name got updated
+       respTenant, _, err := TOSession.Tenant(strconv.Itoa(modTenant.ID))
+       if err != nil {
+               t.Errorf("cannot GET Tenant by name: %v - %v", name, err)
+       }
+       if respTenant.ParentName != parentName {
+               t.Errorf("results do not match actual: %s, expected: %s", 
respTenant.ParentName, parentName)
+       }
+
+}
+
+func DeleteTestTenants(t *testing.T) {
+
+       t1 := "tenant1"
+       tenant1, _, err := TOSession.TenantByName(t1)
+
+       if err != nil {
+               t.Errorf("cannot GET Tenant by name: %v - %v", t1, err)
+       }
+       expectedChildDeleteErrMsg := `Tenant '` + strconv.Itoa(tenant1.ID) + `' 
has child tenants. Please update these child tenants and retry.`
+       if _, err := TOSession.DeleteTenant(strconv.Itoa(tenant1.ID)); err == 
nil {
+               t.Fatalf("%s has child tenants -- should not be able to 
delete", t1)
+       } else if !strings.Contains(err.Error(), expectedChildDeleteErrMsg) {
+               t.Errorf("expected error: %s;  got %s", 
expectedChildDeleteErrMsg, err.Error())
+       }
+
+       deletedTenants := map[string]struct{}{}
+       for {
+               initLenDeleted := len(deletedTenants)
+               for _, tn := range testData.Tenants {
+                       if _, ok := deletedTenants[tn.Name]; ok {
+                               continue
+                       }
+
+                       hasParent := false
+                       for _, otherTenant := range testData.Tenants {
+                               if _, ok := deletedTenants[otherTenant.Name]; 
ok {
+                                       continue
+                               }
+                               if otherTenant.ParentName == tn.Name {
+                                       hasParent = true
+                                       break
+                               }
+                       }
+                       if hasParent {
+                               continue
+                       }
+
+                       toTenant, _, err := TOSession.TenantByName(tn.Name)
+                       if err != nil {
+                               t.Fatalf("getting tenant %s: %v", tn.Name, err)
+                       }
+                       if _, err = 
TOSession.DeleteTenant(strconv.Itoa(toTenant.ID)); err != nil {
+                               t.Fatalf("deleting tenant %s: %v", 
toTenant.Name, err)
+                       }
+                       deletedTenants[tn.Name] = struct{}{}
+
+               }
+               if len(deletedTenants) == len(testData.Tenants) {
+                       break
+               }
+               if len(deletedTenants) == initLenDeleted {
+                       t.Fatal("could not delete tenants: not tenant without 
an existing child found (cycle?)")
+               }
+       }
+}
+
+func TestTenantsActive(t *testing.T) {
+       CreateTestCDNs(t)
+       CreateTestTypes(t)
+       CreateTestTenants(t)
+       CreateTestParameters(t)
+       CreateTestProfiles(t)
+       CreateTestStatuses(t)
+       CreateTestDivisions(t)
+       CreateTestRegions(t)
+       CreateTestPhysLocations(t)
+       CreateTestCacheGroups(t)
+       CreateTestServers(t)
+       CreateTestDeliveryServices(t)
+       CreateTestUsers(t)
+
+       UpdateTestTenantsActive(t)
+
+       ForceDeleteTestUsers(t)
+       DeleteTestDeliveryServices(t)
+       DeleteTestServers(t)
+       DeleteTestCacheGroups(t)
+       DeleteTestPhysLocations(t)
+       DeleteTestRegions(t)
+       DeleteTestDivisions(t)
+       DeleteTestStatuses(t)
+       DeleteTestProfiles(t)
+       DeleteTestParameters(t)
+       DeleteTestTenants(t)
+       DeleteTestTypes(t)
+       DeleteTestCDNs(t)
+}
+
+func UpdateTestTenantsActive(t *testing.T) {
+       originalTenants, _, err := TOSession.Tenants()
+       if err != nil {
+               t.Fatalf("getting tenants error expected: nil, actual: %+v", 
err)
+       }
+
+       setTenantActive(t, "tenant1", true)
+       setTenantActive(t, "tenant2", true)
+       setTenantActive(t, "tenant3", false)
+
+       // ds3 has tenant3. Even though tenant3 is inactive, we should still be 
able to get it, because our user is tenant1, which is active.
+       dses, _, err := TOSession.GetDeliveryServiceByXMLID("ds3")
+       if err != nil {
+               t.Fatal("failed to get delivery service, when the DS's tenant 
was inactive (even though our user's tenant was active)")
+       } else if len(dses) != 1 {
+               t.Error("admin user getting delivery service ds3 with tenant3, 
expected: ds, actual: empty")
+       }
+
+       setTenantActive(t, "tenant1", true)
+       setTenantActive(t, "tenant2", false)
+       setTenantActive(t, "tenant3", true)
+
+       // ds3 has tenant3. Even though tenant3's parent, tenant2, is inactive, 
we should still be able to get it, because our user is tenant1, which is active.
+       _, _, err = TOSession.GetDeliveryServiceByXMLID("ds3")
+       if err != nil {
+               t.Fatal("failed to get delivery service, when a parent tenant 
was inactive (even though our user's tenant was active)")
+       }
+
+       toReqTimeout := time.Second * 
time.Duration(Config.Default.Session.TimeoutInSecs)
+       tenant3Session, _, err := client.LoginWithAgent(TOSession.URL, 
"tenant3user", "pa$$word", true, "to-api-v1-client-tests/tenant3user", 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

Reply via email to