mitchell852 closed pull request #3165: Fix TO missing return URL: https://github.com/apache/trafficcontrol/pull/3165
This is a PR merged from a forked repository. As GitHub hides the original diff on merge, it is displayed below for the sake of provenance: As this is a foreign pull request (from a fork), the diff is supplied below (as it won't show otherwise due to GitHub magic): diff --git a/traffic_ops/testing/api/v14/readonlycannotmodify_test.go b/traffic_ops/testing/api/v14/readonlycannotmodify_test.go new file mode 100644 index 000000000..b718c49af --- /dev/null +++ b/traffic_ops/testing/api/v14/readonlycannotmodify_test.go @@ -0,0 +1,91 @@ +package v14 + +/* + + 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 ( + "strings" + "testing" + "time" + + "github.com/apache/trafficcontrol/lib/go-tc" + toclient "github.com/apache/trafficcontrol/traffic_ops/client" +) + +func TestReadOnlyCannotModify(t *testing.T) { + CreateTestCDNs(t) + CreateTestTypes(t) + CreateTestTenants(t) + CreateTestProfiles(t) + CreateTestStatuses(t) + CreateTestDivisions(t) + CreateTestRegions(t) + CreateTestPhysLocations(t) + CreateTestCacheGroups(t) + CreateTestDeliveryServices(t) + CreateTestUsers(t) + + CreateTestCDNWithReadOnlyUser(t) + + ForceDeleteTestUsers(t) + DeleteTestDeliveryServices(t) + DeleteTestCacheGroups(t) + DeleteTestPhysLocations(t) + DeleteTestRegions(t) + DeleteTestDivisions(t) + DeleteTestStatuses(t) + DeleteTestProfiles(t) + DeleteTestTenants(t) + DeleteTestTypes(t) + DeleteTestCDNs(t) +} + +func CreateTestCDNWithReadOnlyUser(t *testing.T) { + if len(testData.CDNs) == 0 { + t.Fatalf("Can't test readonly user creating a cdns: test data has no cdns\n") + } + + toReqTimeout := time.Second * time.Duration(Config.Default.Session.TimeoutInSecs) + readonlyTOClient, _, err := toclient.LoginWithAgent(TOSession.URL, "readonlyuser", "pa$$word", true, "to-api-v14-client-tests/readonlyuser", true, toReqTimeout) + if err != nil { + t.Fatalf("failed to get log in with readonlyuser: " + err.Error()) + } + + cdn := tc.CDN{ + Name: "cdn-test-readonly-create-failure", + DomainName: "createfailure.invalid", + } + + alerts, _, err := readonlyTOClient.CreateCDN(cdn) + + if err == nil { + t.Errorf("readonlyuser creating cdn error expected: not nil, actual: nil error") + } + + if !strings.Contains(strings.ToLower(err.Error()), "forbidden") { + t.Errorf("readonlyuser creating cdn error expected: contains 'forbidden', actual: '" + err.Error() + "'") + } + + for _, alert := range alerts.Alerts { + if alert.Level == string(tc.SuccessLevel) { + t.Errorf("readonlyuser creating cdn, alerts expected: no success alert, actual: got success alert '" + alert.Text + "'") + } + } + + cdns, _, _ := TOSession.GetCDNByName(cdn.Name) + if len(cdns) > 0 { + t.Errorf("readonlyuser getting created cdn, len(cdns) expected: 0, actual: %+v %++v", len(cdns), cdns) + } +} diff --git a/traffic_ops/testing/api/v14/tc-fixtures.json b/traffic_ops/testing/api/v14/tc-fixtures.json index 00f3b92c3..110ed92a9 100644 --- a/traffic_ops/testing/api/v14/tc-fixtures.json +++ b/traffic_ops/testing/api/v14/tc-fixtures.json @@ -1807,6 +1807,28 @@ "tenant": "tenant1", "uid": 0, "username": "disalloweduser" + }, + { + "addressLine1": "address of readonly", + "addressLine2": "place", + "city": "somewhere", + "company": "else", + "country": "UK", + "email": "[email protected]", + "fullName": "Readonly User", + "gid": 0, + "localPasswd": "pa$$word", + "confirmLocalPasswd": "pa$$word", + "newUser": false, + "phoneNumber": "", + "postalCode": "", + "publicSshKey": "", + "registrationSent": "", + "role": 2, + "stateOrProvince": "", + "tenant": "tenant1", + "uid": 0, + "username": "readonlyuser" } ], "steeringTargets": [ diff --git a/traffic_ops/traffic_ops_golang/wrappers.go b/traffic_ops/traffic_ops_golang/wrappers.go index b06fd5bd0..ef68b5197 100644 --- a/traffic_ops/traffic_ops_golang/wrappers.go +++ b/traffic_ops/traffic_ops_golang/wrappers.go @@ -62,6 +62,7 @@ func (a AuthBase) GetWrapper(privLevelRequired int) Middleware { } if user.PrivLevel < privLevelRequired { api.HandleErr(w, r, nil, http.StatusForbidden, errors.New("Forbidden."), nil) + return } api.AddUserToReq(r, user) handlerFunc(w, r) ---------------------------------------------------------------- This is an automated message from the Apache Git Service. To respond to the message, please log on 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
