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/04/18 15:48:34 UTC

[incubator-trafficcontrol] branch master updated: Add some authentication API integration 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/incubator-trafficcontrol.git


The following commit(s) were added to refs/heads/master by this push:
     new d13f91e  Add some authentication API integration tests
d13f91e is described below

commit d13f91e9099a34f818cf4d14ec503a0efab35d42
Author: Rawlin Peters <ra...@comcast.com>
AuthorDate: Tue Apr 17 17:19:45 2018 -0600

    Add some authentication API integration tests
    
    Added tests for the /ping endpoint to make sure it does not require
    authentication. Added tests for some other endpoints as well to show
    examples of how to test authentication.
---
 traffic_ops/client/v13/session.go               | 11 +++++++
 traffic_ops/testing/api/utils/utils.go          |  5 +++
 traffic_ops/testing/api/v13/about_test.go       |  5 +++
 traffic_ops/testing/api/v13/cachegroups_test.go | 41 +++++++++++++++++++++++++
 traffic_ops/testing/api/v13/ping_test.go        | 10 +++++-
 traffic_ops/testing/api/v13/session.go          |  4 ++-
 traffic_ops/traffic_ops_golang/routes.go        |  2 +-
 7 files changed, 75 insertions(+), 3 deletions(-)

diff --git a/traffic_ops/client/v13/session.go b/traffic_ops/client/v13/session.go
index 79b0368..aea65d6 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 f99e850..8de3279 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 8e64c0c..b36f9f9 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 e43b24a..1d64361 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 9091223..8da9433 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 8b61585..90f29f4 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 36046a7..4aa4dcf 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},

-- 
To stop receiving notification emails like this one, please contact
dewrich@apache.org.