You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@trafficcontrol.apache.org by de...@apache.org on 2018/06/26 18:54:57 UTC

[trafficcontrol] 06/15: add tenants API tests

This is an automated email from the ASF dual-hosted git repository.

dewrich pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/trafficcontrol.git

commit ba9f6f09e39ea9d568be798074bede3d1f15ede2
Author: Dan Kirkwood <da...@apache.org>
AuthorDate: Thu Jun 21 14:12:18 2018 -0600

    add tenants API tests
---
 .../api/v13/deliveryservice_requests_test.go       |   1 +
 traffic_ops/testing/api/v13/tc-fixtures.json       | 147 ++++++++++++++++++++-
 traffic_ops/testing/api/v13/tenants_test.go        | 136 +++++++++++++++++++
 traffic_ops/testing/api/v13/todb.go                |  28 ++--
 traffic_ops/testing/api/v13/traffic_control.go     |   2 +-
 5 files changed, 292 insertions(+), 22 deletions(-)

diff --git a/traffic_ops/testing/api/v13/deliveryservice_requests_test.go b/traffic_ops/testing/api/v13/deliveryservice_requests_test.go
index 6d974ae..1b6589a 100644
--- a/traffic_ops/testing/api/v13/deliveryservice_requests_test.go
+++ b/traffic_ops/testing/api/v13/deliveryservice_requests_test.go
@@ -39,6 +39,7 @@ func TestDeliveryServiceRequests(t *testing.T) {
 	GetTestDeliveryServiceRequests(t)
 	UpdateTestDeliveryServiceRequests(t)
 	DeleteTestDeliveryServiceRequests(t)
+
 	DeleteTestTypes(t)
 	DeleteTestCDNs(t)
 
diff --git a/traffic_ops/testing/api/v13/tc-fixtures.json b/traffic_ops/testing/api/v13/tc-fixtures.json
index c41eb8b..21f61f9 100644
--- a/traffic_ops/testing/api/v13/tc-fixtures.json
+++ b/traffic_ops/testing/api/v13/tc-fixtures.json
@@ -1043,18 +1043,13 @@
     "tenants": [
         {
             "active": true,
-            "name": "root",
-            "parentTenantName": null
-        },
-        {
-            "active": true,
             "name": "tenant1",
-            "parentTenantName": "root"
+            "parentName": "root"
         },
         {
             "active": false,
             "name": "tenant2",
-            "parentTenantName": "root"
+            "parentName": "root"
         }
     ],
     "types": [
@@ -1250,5 +1245,143 @@
             "name": "TRAFFIC_MONITOR",
             "useInTable": "server"
         }
+    ],
+    "users": [
+        {
+            "addressLine1": "address of admin",
+            "addressLine2": "",
+            "city": "Anywhere",
+            "company": "Comcast",
+            "country": "USA",
+            "email": "admin@example.com",
+            "fullName": "Fred the admin",
+            "gid": 0,
+            "localPasswd": "pa$$word",
+            "newUser": false,
+            "phoneNumber": "810-555-9876",
+            "postalCode": "55443",
+            "publicSshKey": "",
+            "role": 4,
+            "rolename": "admin",
+            "stateOrProvince": "LA",
+            "tenant": "root",
+            "uid": 0,
+            "username": "adminuser"
+        },
+        {
+            "addressLine1": "address of disallowed",
+            "addressLine2": "place",
+            "city": "somewhere",
+            "company": "else",
+            "country": "UK",
+            "email": "disallowed@example.com",
+            "fullName": "Me me",
+            "gid": 0,
+            "localPasswd": "pa$$word",
+            "newUser": false,
+            "phoneNumber": "",
+            "postalCode": "",
+            "publicSshKey": "",
+            "registrationSent": "",
+            "role": 1,
+            "roleName": "disallowed",
+            "stateOrProvince": "",
+            "tenant": "root",
+            "tenant": 3,
+            "uid": 0,
+            "username": "disalloweduser"
+        },
+        {
+            "addressLine1": "address of read-only",
+            "addressLine2": "",
+            "city": "",
+            "company": "",
+            "country": "",
+            "email": "readonly@example.com",
+            "fullName": "",
+            "gid": 0,
+            "localPasswd": "pa$$word",
+            "newUser": false,
+            "phoneNumber": "",
+            "postalCode": "",
+            "publicSshKey": "",
+            "registrationSent": "",
+            "role": 2,
+            "roleName": "read-only user",
+            "stateOrProvince": "",
+            "tenant": "root",
+            "tenant": 3,
+            "uid": 0,
+            "username": "readuser"
+        },
+        {
+            "addressLine1": "address of operations",
+            "addressLine2": "",
+            "city": "",
+            "company": "",
+            "country": "",
+            "email": "operations@example.com",
+            "fullName": "",
+            "gid": 0,
+            "localPasswd": "pa$$word",
+            "newUser": false,
+            "phoneNumber": "",
+            "postalCode": "",
+            "publicSshKey": "",
+            "registrationSent": "",
+            "role": 3,
+            "roleName": "operations",
+            "stateOrProvince": "",
+            "tenant": "root",
+            "tenant": 4,
+            "uid": 0,
+            "username": "operationsuser"
+        },
+        {
+            "addressLine1": "",
+            "addressLine2": "",
+            "city": "",
+            "company": "",
+            "country": "",
+            "email": "portal@example.com",
+            "fullName": "",
+            "gid": 0,
+            "localPasswd": "pa$$word",
+            "newUser": false,
+            "phoneNumber": "",
+            "postalCode": "",
+            "publicSshKey": "",
+            "registrationSent": "",
+            "role": 5,
+            "roleName": "portal",
+            "stateOrProvince": "",
+            "tenant": "root",
+            "tenant": 2,
+            "uid": 0,
+            "username": "portaluser"
+        },
+        {
+            "addressLine1": "",
+            "addressLine2": "",
+            "city": "",
+            "company": "",
+            "country": "",
+            "email": "federation@example.com",
+            "fullName": "",
+            "gid": 0,
+            "localPasswd": "pa$$word",
+            "newUser": false,
+            "phoneNumber": "",
+            "postalCode": "",
+            "publicSshKey": "",
+            "registrationSent": "",
+            "role": 7,
+            "roleName": "federation",
+            "stateOrProvince": "",
+            "tenant": "root",
+            "tenant": 4,
+            "uid": 0,
+            "username": "federationuser"
+        }
     ]
 }
diff --git a/traffic_ops/testing/api/v13/tenants_test.go b/traffic_ops/testing/api/v13/tenants_test.go
new file mode 100644
index 0000000..feb205c
--- /dev/null
+++ b/traffic_ops/testing/api/v13/tenants_test.go
@@ -0,0 +1,136 @@
+package v13
+
+/*
+
+   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 (
+	"testing"
+
+	tc "github.com/apache/trafficcontrol/lib/go-tc"
+)
+
+func TestTenants(t *testing.T) {
+
+	CreateTestTenants(t)
+	UpdateTestTenants(t)
+	GetTestTenants(t)
+	//DeleteTestTenants(t)
+}
+
+func CreateTestTenants(t *testing.T) {
+	for _, ten := range testData.Tenants {
+		// testData does not define ParentID -- look up by name and fill in
+		if ten.ParentID == 0 {
+			parents, _, err := TOSession.GetTenantByName(ten.ParentName)
+			if err != nil {
+				t.Errorf("parent tenant %s: %++v", ten.ParentName, err)
+				continue
+			}
+			ten.ParentID = parents[0].ID
+		}
+		resp, _, err := TOSession.CreateTenant(ten)
+		t.Logf("response: %++v", resp)
+
+		if err != nil {
+			t.Errorf("could not CREATE tenant %s: %v\n", ten.Name, err)
+		}
+	}
+}
+
+func GetTestTenants(t *testing.T) {
+	resp, _, err := TOSession.GetTenants()
+	if err != nil {
+		t.Errorf("cannot GET all tenants: %v - %v\n", err, resp)
+		return
+	}
+
+	// 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 {
+		resp, _, err := TOSession.GetTenantByName(ten.Name)
+		if err != nil {
+			t.Errorf("cannot GET Tenant by name: %v - %v\n", err, resp)
+			continue
+		}
+		if len(resp) != 1 {
+			t.Errorf("expected 1 tenant for %s,  got %d", ten.Name, len(resp))
+			continue
+		}
+	}
+}
+
+func UpdateTestTenants(t *testing.T) {
+
+	// Retrieve the Tenant by name so we can get the id for the Update
+	name := "tenant2"
+	parentName := "tenant1"
+	resp, _, err := TOSession.GetTenantByName(name)
+	if err != nil {
+		t.Errorf("cannot GET Tenant by name: %s - %v\n", name, err)
+	}
+	modTenant := resp[0]
+	resp, _, err = TOSession.GetTenantByName(parentName)
+	if err != nil {
+		t.Errorf("cannot GET Tenant by name: %s - %v\n", name, err)
+	}
+	newParent := resp[0]
+	modTenant.ParentID = newParent.ID
+	var alert tc.Alerts
+	alert, _, err = TOSession.UpdateTenantByID(modTenant.ID, modTenant)
+	if err != nil {
+		t.Errorf("cannot UPDATE Tenant by id: %v - %v\n", err, alert)
+	}
+
+	// Retrieve the Tenant to check Tenant parent name got updated
+	resp, _, err = TOSession.GetTenantByID(modTenant.ID)
+	if err != nil {
+		t.Errorf("cannot GET Tenant by name: %v - %v\n", name, err)
+	}
+	t.Logf("modified: %++v", resp)
+	respTenant := resp[0]
+	if respTenant.ParentName != parentName {
+		t.Errorf("results do not match actual: %s, expected: %s\n", respTenant.ParentName, parentName)
+	}
+
+}
+
+func DeleteTestTenants(t *testing.T) {
+
+	for _, ten := range testData.Tenants {
+		// Retrieve the Tenant by name so we can get the id for the Update
+		resp, _, err := TOSession.GetTenantByName(ten.Name)
+		if err != nil {
+			t.Errorf("cannot GET Tenant by name: %v - %v\n", ten.Name, err)
+		}
+		respTenant := resp[0]
+
+		delResp, _, err := TOSession.DeleteTenantByID(respTenant.ID)
+		if err != nil {
+			t.Errorf("cannot DELETE Tenant by name: %v - %v\n", err, delResp)
+		}
+
+		// Retrieve the Tenant to see if it got deleted
+		Tenants, _, err := TOSession.GetTenantByName(ten.Name)
+		if err != nil {
+			t.Errorf("error deleting Tenant name: %s\n", err.Error())
+		}
+		if len(Tenants) > 0 {
+			t.Errorf("expected Tenant name: %s to be deleted\n", ten.Name)
+		}
+	}
+}
diff --git a/traffic_ops/testing/api/v13/todb.go b/traffic_ops/testing/api/v13/todb.go
index 0081ee8..0ebea91 100644
--- a/traffic_ops/testing/api/v13/todb.go
+++ b/traffic_ops/testing/api/v13/todb.go
@@ -49,12 +49,6 @@ func OpenConnection() (*sql.DB, error) {
 func SetupTestData(*sql.DB) error {
 	var err error
 
-	err = SetupTenants(db)
-	if err != nil {
-		fmt.Printf("\nError setting up tenants %s - %s, %v\n", Config.TrafficOps.URL, Config.TrafficOps.Users.Admin, err)
-		os.Exit(1)
-	}
-
 	err = SetupRoles(db)
 	if err != nil {
 		fmt.Printf("\nError setting up roles %s - %s, %v\n", Config.TrafficOps.URL, Config.TrafficOps.Users.Admin, err)
@@ -73,6 +67,12 @@ func SetupTestData(*sql.DB) error {
 		os.Exit(1)
 	}
 
+	err = SetupTenants(db)
+	if err != nil {
+		fmt.Printf("\nError setting up tenant %s - %s, %v\n", Config.TrafficOps.URL, Config.TrafficOps.Users.Admin, err)
+		os.Exit(1)
+	}
+
 	err = SetupTmusers(db)
 	if err != nil {
 		fmt.Printf("\nError setting up tm_user %s - %s, %v\n", Config.TrafficOps.URL, Config.TrafficOps.Users.Admin, err)
@@ -136,12 +136,12 @@ func SetupTmusers(db *sql.DB) error {
 
 	// Creates users in different tenants
 	sqlStmt := `
-INSERT INTO tm_user (username, local_passwd, confirm_local_passwd, role, tenant_id) VALUES ('` + Config.TrafficOps.Users.Disallowed + `','` + encryptedPassword + `','` + encryptedPassword + `', 1, 3);
-INSERT INTO tm_user (username, local_passwd, confirm_local_passwd, role, tenant_id) VALUES ('` + Config.TrafficOps.Users.ReadOnly + `','` + encryptedPassword + `','` + encryptedPassword + `', 2, 3);
-INSERT INTO tm_user (username, local_passwd, confirm_local_passwd, role, tenant_id) VALUES ('` + Config.TrafficOps.Users.Operations + `','` + encryptedPassword + `','` + encryptedPassword + `', 3, 3);
+INSERT INTO tm_user (username, local_passwd, confirm_local_passwd, role, tenant_id) VALUES ('` + Config.TrafficOps.Users.Disallowed + `','` + encryptedPassword + `','` + encryptedPassword + `', 1, 2);
+INSERT INTO tm_user (username, local_passwd, confirm_local_passwd, role, tenant_id) VALUES ('` + Config.TrafficOps.Users.ReadOnly + `','` + encryptedPassword + `','` + encryptedPassword + `', 2, 2);
+INSERT INTO tm_user (username, local_passwd, confirm_local_passwd, role, tenant_id) VALUES ('` + Config.TrafficOps.Users.Operations + `','` + encryptedPassword + `','` + encryptedPassword + `', 3, 2);
 INSERT INTO tm_user (username, local_passwd, confirm_local_passwd, role, tenant_id) VALUES ('` + Config.TrafficOps.Users.Admin + `','` + encryptedPassword + `','` + encryptedPassword + `', 4, 2);
-INSERT INTO tm_user (username, local_passwd, confirm_local_passwd, role, tenant_id) VALUES ('` + Config.TrafficOps.Users.Portal + `','` + encryptedPassword + `','` + encryptedPassword + `', 5, 3);
-INSERT INTO tm_user (username, local_passwd, confirm_local_passwd, role, tenant_id) VALUES ('` + Config.TrafficOps.Users.Federation + `','` + encryptedPassword + `','` + encryptedPassword + `', 6, 3);
+INSERT INTO tm_user (username, local_passwd, confirm_local_passwd, role, tenant_id) VALUES ('` + Config.TrafficOps.Users.Portal + `','` + encryptedPassword + `','` + encryptedPassword + `', 5, 2);
+INSERT INTO tm_user (username, local_passwd, confirm_local_passwd, role, tenant_id) VALUES ('` + Config.TrafficOps.Users.Federation + `','` + encryptedPassword + `','` + encryptedPassword + `', 6, 2);
 `
 	err = execSQL(db, sqlStmt, "tm_user")
 	if err != nil {
@@ -153,11 +153,11 @@ INSERT INTO tm_user (username, local_passwd, confirm_local_passwd, role, tenant_
 // SetupTenants ...
 func SetupTenants(db *sql.DB) error {
 
+	// TODO: root tenant must be present in initial database.  "badtenant" is needed for now so tests can be done
+	// with a tenant outside the user's tenant.  That should be removed once User API tests are in place rather than the SetupUsers defined above.
 	sqlStmt := `
 INSERT INTO tenant (id, name, active, parent_id, last_updated) VALUES (1, 'root', true, null, '2018-01-19 19:01:21.327262');
-INSERT INTO tenant (id, name, active, parent_id, last_updated) VALUES (2, 'grandparent tenant', true, 1, '2018-01-19 19:01:21.327262');
-INSERT INTO tenant (id, name, active, parent_id, last_updated) VALUES (3, 'parent tenant', true, 2, '2018-01-19 19:01:21.327262');
-INSERT INTO tenant (id, name, active, parent_id, last_updated) VALUES (4, 'child tenant', true, 3, '2018-01-19 19:01:21.327262');
+INSERT INTO tenant (id, name, active, parent_id, last_updated) VALUES (2, 'badtenant', true, 1, '2018-01-19 19:01:21.327262');
 `
 	err := execSQL(db, sqlStmt, "tenant")
 	if err != nil {
diff --git a/traffic_ops/testing/api/v13/traffic_control.go b/traffic_ops/testing/api/v13/traffic_control.go
index 98fcdfc..bbe7d13 100644
--- a/traffic_ops/testing/api/v13/traffic_control.go
+++ b/traffic_ops/testing/api/v13/traffic_control.go
@@ -39,6 +39,6 @@ type TrafficControl struct {
 	Roles                          []v13.Role                          `json:"roles"`
 	Servers                        []v13.Server                        `json:"servers"`
 	Statuses                       []v12.Status                        `json:"statuses"`
-	Tenants                        []v12.Tenant                        `json:"tenants"`
+	Tenants                        []v13.Tenant                        `json:"tenants"`
 	Types                          []v12.Type                          `json:"types"`
 }