You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@trafficcontrol.apache.org by GitBox <gi...@apache.org> on 2018/04/18 15:48:34 UTC

[GitHub] dewrich closed pull request #2144: Add some authentication API integration tests

dewrich closed pull request #2144: Add some authentication API integration tests
URL: https://github.com/apache/incubator-trafficcontrol/pull/2144
 
 
   

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/client/v13/session.go b/traffic_ops/client/v13/session.go
index 79b036886..aea65d681 100644
--- a/traffic_ops/client/v13/session.go
+++ b/traffic_ops/client/v13/session.go
@@ -233,6 +233,17 @@ func LogoutWithAgent(toURL string, toUser string, toPasswd string, insecure bool
 	return to, remoteAddr, nil
 }
 
+// NewNoAuthSession returns a new Session without logging in
+// this can be used for querying unauthenticated endpoints without requiring a login
+func NewNoAuthSession(toURL string, insecure bool, userAgent string, useCache bool, requestTimeout time.Duration) *Session {
+	return NewSession("", "", toURL, userAgent, &http.Client{
+		Timeout: requestTimeout,
+		Transport: &http.Transport{
+			TLSClientConfig: &tls.Config{InsecureSkipVerify: insecure},
+		},
+	}, useCache)
+}
+
 // ErrUnlessOk returns nil and an error if the given Response's status code is anything but 200 OK. This includes reading the Response.Body and Closing it. Otherwise, the given response and error are returned unchanged.
 func (to *Session) ErrUnlessOK(resp *http.Response, remoteAddr net.Addr, err error, path string) (*http.Response, net.Addr, error) {
 	if err != nil {
diff --git a/traffic_ops/testing/api/utils/utils.go b/traffic_ops/testing/api/utils/utils.go
index f99e850c2..8de3279b3 100644
--- a/traffic_ops/testing/api/utils/utils.go
+++ b/traffic_ops/testing/api/utils/utils.go
@@ -21,6 +21,11 @@ import (
 	"testing"
 )
 
+type ErrorAndMessage struct {
+	Error   error
+	Message string
+}
+
 func FindNeedle(needle string, haystack []string) bool {
 	found := false
 	for _, s := range haystack {
diff --git a/traffic_ops/testing/api/v13/about_test.go b/traffic_ops/testing/api/v13/about_test.go
index 8e64c0cca..b36f9f98e 100644
--- a/traffic_ops/testing/api/v13/about_test.go
+++ b/traffic_ops/testing/api/v13/about_test.go
@@ -25,4 +25,9 @@ func TestAbout(t *testing.T) {
 		t.Errorf("error from GetAbout(): %v", err)
 	}
 	t.Logf("about: %v", m)
+
+	m, _, err = NoAuthTOSession.GetAbout()
+	if err == nil {
+		t.Error("expected error from GetAbout() when unauthenticated")
+	}
 }
diff --git a/traffic_ops/testing/api/v13/cachegroups_test.go b/traffic_ops/testing/api/v13/cachegroups_test.go
index e43b24a68..1d64361a5 100644
--- a/traffic_ops/testing/api/v13/cachegroups_test.go
+++ b/traffic_ops/testing/api/v13/cachegroups_test.go
@@ -16,11 +16,13 @@ package v13
 */
 
 import (
+	"fmt"
 	"testing"
 
 	"github.com/apache/incubator-trafficcontrol/lib/go-log"
 	tc "github.com/apache/incubator-trafficcontrol/lib/go-tc"
 	"github.com/apache/incubator-trafficcontrol/lib/go-tc/v13"
+	"github.com/apache/incubator-trafficcontrol/traffic_ops/testing/api/utils"
 )
 
 func TestCacheGroups(t *testing.T) {
@@ -30,6 +32,7 @@ func TestCacheGroups(t *testing.T) {
 	UpdateTestCacheGroups(t)
 	DeleteTestCacheGroups(t)
 	DeleteTestTypes(t)
+	TestCacheGroupsAuthentication(t)
 }
 
 func CreateTestCacheGroups(t *testing.T) {
@@ -181,3 +184,41 @@ func DeleteTestCacheGroups(t *testing.T) {
 		log.Debugln("DeleteTestCacheGroups() PASSED: ")
 	}
 }
+
+func TestCacheGroupsAuthentication(t *testing.T) {
+	failed := false
+	errFormat := "expected error from %s when unauthenticated"
+
+	cg := testData.CacheGroups[0]
+
+	errors := make([]utils.ErrorAndMessage, 0)
+
+	_, _, err := NoAuthTOSession.CreateCacheGroup(cg)
+	errors = append(errors, utils.ErrorAndMessage{err, fmt.Sprintf(errFormat, "CreateCacheGroup")})
+
+	_, _, err = NoAuthTOSession.GetCacheGroups()
+	errors = append(errors, utils.ErrorAndMessage{err, fmt.Sprintf(errFormat, "GetCacheGroups")})
+
+	_, _, err = NoAuthTOSession.GetCacheGroupByName(cg.Name)
+	errors = append(errors, utils.ErrorAndMessage{err, fmt.Sprintf(errFormat, "GetCacheGroupByName")})
+
+	_, _, err = NoAuthTOSession.GetCacheGroupByID(cg.ID)
+	errors = append(errors, utils.ErrorAndMessage{err, fmt.Sprintf(errFormat, "GetCacheGroupByID")})
+
+	_, _, err = NoAuthTOSession.UpdateCacheGroupByID(cg.ID, cg)
+	errors = append(errors, utils.ErrorAndMessage{err, fmt.Sprintf(errFormat, "UpdateCacheGroupByID")})
+
+	_, _, err = NoAuthTOSession.DeleteCacheGroupByID(cg.ID)
+	errors = append(errors, utils.ErrorAndMessage{err, fmt.Sprintf(errFormat, "DeleteCacheGroupByID")})
+
+	for _, err := range errors {
+		if err.Error == nil {
+			t.Error(err.Message)
+			failed = true
+		}
+	}
+
+	if !failed {
+		log.Debugln("TestCacheGroupsAuthentication() PASSED: ")
+	}
+}
diff --git a/traffic_ops/testing/api/v13/ping_test.go b/traffic_ops/testing/api/v13/ping_test.go
index 9091223fc..8da943367 100644
--- a/traffic_ops/testing/api/v13/ping_test.go
+++ b/traffic_ops/testing/api/v13/ping_test.go
@@ -20,5 +20,13 @@ import (
 )
 
 func TestPing(t *testing.T) {
-	TOSession.Ping()
+	_, _, err := TOSession.Ping()
+	if err != nil {
+		t.Errorf("could not Ping authenticated: %v\n", err)
+	}
+
+	_, _, err = NoAuthTOSession.Ping()
+	if err != nil {
+		t.Errorf("could not Ping unauthenticated: %v\n", err)
+	}
 }
diff --git a/traffic_ops/testing/api/v13/session.go b/traffic_ops/testing/api/v13/session.go
index 8b615858a..90f29f474 100644
--- a/traffic_ops/testing/api/v13/session.go
+++ b/traffic_ops/testing/api/v13/session.go
@@ -23,13 +23,15 @@ import (
 )
 
 var (
-	TOSession *v13.Session
+	TOSession       *v13.Session
+	NoAuthTOSession *v13.Session
 )
 
 func SetupSession(toReqTimeout time.Duration, toURL string, toUser string, toPass string) error {
 	var err error
 
 	toReqTimeout = time.Second * time.Duration(Config.Default.Session.TimeoutInSecs)
+	NoAuthTOSession = v13.NewNoAuthSession(toURL, true, "to-api-v13-client-tests", true, toReqTimeout)
 	TOSession, _, err = v13.LoginWithAgent(toURL, toUser, toPass, true, "to-api-v13-client-tests", true, toReqTimeout)
 	return err
 }
diff --git a/traffic_ops/traffic_ops_golang/routes.go b/traffic_ops/traffic_ops_golang/routes.go
index 36046a726..4aa4dcff4 100644
--- a/traffic_ops/traffic_ops_golang/routes.go
+++ b/traffic_ops/traffic_ops_golang/routes.go
@@ -179,7 +179,7 @@ func Routes(d ServerData) ([]Route, http.Handler, error) {
 		{1.3, http.MethodDelete, `parameters/{id}$`, api.DeleteHandler(parameter.GetRefType(), d.DB), auth.PrivLevelOperations, Authenticated, nil},
 
 		//Ping
-		{1.2, http.MethodGet, `ping$`, ping.PingHandler(), auth.PrivLevelReadOnly, NoAuth, nil},
+		{1.2, http.MethodGet, `ping$`, ping.PingHandler(), 0, NoAuth, nil},
 
 		//Servers
 		{1.2, http.MethodGet, `servers/?(\.json)?$`, api.ReadHandler(server.GetRefType(), d.DB), auth.PrivLevelReadOnly, Authenticated, nil},


 

----------------------------------------------------------------
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:
users@infra.apache.org


With regards,
Apache Git Services