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

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


The following commit(s) were added to refs/heads/master by this push:
     new 2cc067b  Adding additional tests for snapshot endpoints (#5677)
2cc067b is described below

commit 2cc067b824a2d606598773bd5f5e4913aba4a68e
Author: Srijeet Chatterjee <[email protected]>
AuthorDate: Tue Mar 30 10:27:24 2021 -0600

    Adding additional tests for snapshot endpoints (#5677)
---
 traffic_ops/testing/api/v4/crconfig_test.go | 49 +++++++++++++++++++++++++++++
 traffic_ops/testing/api/v4/user_test.go     | 28 +++++++++++++++++
 2 files changed, 77 insertions(+)

diff --git a/traffic_ops/testing/api/v4/crconfig_test.go 
b/traffic_ops/testing/api/v4/crconfig_test.go
index 6d44eca..83c3c8b 100644
--- a/traffic_ops/testing/api/v4/crconfig_test.go
+++ b/traffic_ops/testing/api/v4/crconfig_test.go
@@ -17,9 +17,13 @@ package v4
 
 import (
        "encoding/json"
+       "net/http"
        "testing"
+       "time"
 
        "github.com/apache/trafficcontrol/lib/go-tc"
+       "github.com/apache/trafficcontrol/lib/go-util"
+       toclient "github.com/apache/trafficcontrol/traffic_ops/v4-client"
 )
 
 func TestCRConfig(t *testing.T) {
@@ -29,9 +33,54 @@ func TestCRConfig(t *testing.T) {
                SnapshotTestCDNbyInvalidName(t)
                SnapshotTestCDNbyID(t)
                SnapshotTestCDNbyInvalidID(t)
+               SnapshotWithReadOnlyUser(t)
        })
 }
 
+func SnapshotWithReadOnlyUser(t *testing.T) {
+       if len(testData.CDNs) == 0 {
+               t.Fatalf("expected one or more valid CDNs, but got none")
+       }
+       resp, _, err := TOSession.TenantByNameWithHdr("root", nil)
+       if err != nil {
+               t.Fatalf("couldn't get the root tenant ID: %v", err)
+       }
+       if resp == nil {
+               t.Fatalf("expected a valid tenant response, but got nothing")
+       }
+
+       toReqTimeout := time.Second * 
time.Duration(Config.Default.Session.TimeoutInSecs)
+       user := tc.User{
+               Username:             util.StrPtr("test_user"),
+               RegistrationSent:     tc.TimeNoModFromTime(time.Now()),
+               LocalPassword:        util.StrPtr("test_pa$$word"),
+               ConfirmLocalPassword: util.StrPtr("test_pa$$word"),
+               RoleName:             util.StrPtr("read-only user"),
+       }
+       user.Email = util.StrPtr("[email protected]")
+       user.TenantID = util.IntPtr(resp.ID)
+       user.FullName = util.StrPtr("firstName LastName")
+
+       u, _, err := TOSession.CreateUser(&user)
+       if err != nil {
+               t.Fatalf("could not create read-only user: %v", err)
+       }
+       client, _, err := toclient.LoginWithAgent(TOSession.URL, "test_user", 
"test_pa$$word", true, "to-api-v4-client-tests/tenant4user", true, toReqTimeout)
+       if err != nil {
+               t.Fatalf("failed to log in with test_user: %v", err.Error())
+       }
+       reqInf, err := client.SnapshotCRConfigWithHdr(testData.CDNs[0].Name, 
nil)
+       if err == nil {
+               t.Errorf("expected to get an error about a read-only client 
trying to snap a CDN, but got none")
+       }
+       if reqInf.StatusCode != http.StatusForbidden {
+               t.Errorf("expected a 403 forbidden status code, but got %d", 
reqInf.StatusCode)
+       }
+       if u != nil && u.Response.Username != nil {
+               ForceDeleteTestUsersByUsernames(t, []string{"test_user"})
+       }
+}
+
 func UpdateTestCRConfigSnapshot(t *testing.T) {
        if len(testData.CDNs) < 1 {
                t.Error("no cdn test data")
diff --git a/traffic_ops/testing/api/v4/user_test.go 
b/traffic_ops/testing/api/v4/user_test.go
index 28ccf37..d761708 100644
--- a/traffic_ops/testing/api/v4/user_test.go
+++ b/traffic_ops/testing/api/v4/user_test.go
@@ -507,6 +507,34 @@ func ForceDeleteTestUsers(t *testing.T) {
        }
 }
 
+func ForceDeleteTestUsersByUsernames(t *testing.T, usernames []string) {
+
+       // NOTE: Special circumstances!  This should *NOT* be done without a 
really good reason!
+       //  Connects directly to the DB to remove users rather than going thru 
the client.
+       //  This is required here because the DeleteUser action does not really 
delete users,  but disables them.
+       db, err := OpenConnection()
+       if err != nil {
+               t.Error("cannot open db")
+       }
+       defer db.Close()
+
+       for i, u := range usernames {
+               usernames[i] = `'` + u + `'`
+       }
+       // there is a constraint that prevents users from being deleted when 
they have a log
+       q := `DELETE FROM log WHERE NOT tm_user = (SELECT id FROM tm_user WHERE 
username = 'admin')`
+       err = execSQL(db, q)
+       if err != nil {
+               t.Errorf("cannot execute SQL: %s; SQL is %s", err.Error(), q)
+       }
+
+       q = `DELETE FROM tm_user WHERE username IN (` + strings.Join(usernames, 
",") + `)`
+       err = execSQL(db, q)
+       if err != nil {
+               t.Errorf("cannot execute SQL: %s; SQL is %s", err.Error(), q)
+       }
+}
+
 func DeleteTestUsers(t *testing.T) {
        for _, user := range testData.Users {
 

Reply via email to