github.com/niedbalski/juju@v0.0.0-20190215020005-8ff100488e47/apiserver/common/common_test.go (about)

     1  // Copyright 2013 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package common_test
     5  
     6  import (
     7  	"fmt"
     8  	stdtesting "testing"
     9  
    10  	jc "github.com/juju/testing/checkers"
    11  	"github.com/juju/utils"
    12  	gc "gopkg.in/check.v1"
    13  	"gopkg.in/juju/names.v2"
    14  
    15  	"github.com/juju/juju/apiserver/common"
    16  	coretesting "github.com/juju/juju/testing"
    17  )
    18  
    19  func TestAll(t *stdtesting.T) {
    20  	coretesting.MgoTestPackage(t)
    21  }
    22  
    23  type commonSuite struct{}
    24  
    25  var _ = gc.Suite(&commonSuite{})
    26  
    27  func errorAuth() (common.AuthFunc, error) {
    28  	return nil, fmt.Errorf("pow")
    29  }
    30  
    31  func fooAuth() (common.AuthFunc, error) {
    32  	return func(tag names.Tag) bool {
    33  		return tag == names.NewUserTag("foo")
    34  	}, nil
    35  }
    36  
    37  func barAuth() (common.AuthFunc, error) {
    38  	return func(tag names.Tag) bool {
    39  		return tag == names.NewUserTag("bar")
    40  	}, nil
    41  }
    42  
    43  func bazAuth() (common.AuthFunc, error) {
    44  	return func(tag names.Tag) bool {
    45  		return tag == names.NewUserTag("baz")
    46  	}, nil
    47  }
    48  
    49  var authEitherTests = []struct {
    50  	about  string
    51  	a, b   func() (common.AuthFunc, error)
    52  	tag    names.Tag
    53  	expect bool
    54  	err    string
    55  }{{
    56  	about: "a returns an error",
    57  	a:     errorAuth,
    58  	b:     fooAuth,
    59  	err:   "pow",
    60  }, {
    61  	about: "b returns an error",
    62  	a:     fooAuth,
    63  	b:     errorAuth,
    64  	err:   "pow",
    65  }, {
    66  	about: "both a and b return an error",
    67  	a:     errorAuth,
    68  	b:     errorAuth,
    69  	err:   "pow",
    70  }, {
    71  	about:  "tag foo - a returns true",
    72  	a:      fooAuth,
    73  	b:      barAuth,
    74  	tag:    names.NewUserTag("foo"),
    75  	expect: true,
    76  }, {
    77  	about:  "tag foo - b returns true",
    78  	a:      barAuth,
    79  	b:      fooAuth,
    80  	tag:    names.NewUserTag("foo"),
    81  	expect: true,
    82  }, {
    83  	about:  "tag bar - b returns true",
    84  	a:      fooAuth,
    85  	b:      barAuth,
    86  	tag:    names.NewUserTag("bar"),
    87  	expect: true,
    88  }, {
    89  	about:  "tag foo - both return true",
    90  	a:      fooAuth,
    91  	b:      fooAuth,
    92  	tag:    names.NewUserTag("foo"),
    93  	expect: true,
    94  }, {
    95  	about:  "tag baz - both return false",
    96  	a:      fooAuth,
    97  	b:      barAuth,
    98  	tag:    names.NewUserTag("baz"),
    99  	expect: false,
   100  }, {
   101  	about:  "tag quxx - both return false",
   102  	a:      fooAuth,
   103  	b:      barAuth,
   104  	tag:    names.NewApplicationTag("quxx"),
   105  	expect: false,
   106  }}
   107  
   108  func (s *commonSuite) TestAuthAnyCoversEither(c *gc.C) {
   109  	for i, test := range authEitherTests {
   110  		c.Logf("test %d: %s", i, test.about)
   111  		authAny := common.AuthAny(test.a, test.b)
   112  		any, err := authAny()
   113  		if test.err == "" {
   114  			c.Assert(err, jc.ErrorIsNil)
   115  			ok := any(test.tag)
   116  			c.Assert(ok, gc.Equals, test.expect)
   117  		} else {
   118  			c.Assert(err, gc.ErrorMatches, test.err)
   119  			c.Assert(any, gc.IsNil)
   120  		}
   121  	}
   122  }
   123  
   124  func (s *commonSuite) TestAuthAnyAlwaysFalseWithNoFuncs(c *gc.C) {
   125  	getAuth := common.AuthAny()
   126  	auth, err := getAuth()
   127  	c.Assert(err, jc.ErrorIsNil)
   128  	c.Assert(auth(names.NewUserTag("foo")), jc.IsFalse)
   129  }
   130  
   131  func (s *commonSuite) TestAuthAnyWith3(c *gc.C) {
   132  	getAuth := common.AuthAny(fooAuth, barAuth, bazAuth)
   133  	auth, err := getAuth()
   134  	c.Assert(err, jc.ErrorIsNil)
   135  	c.Check(auth(names.NewUserTag("foo")), jc.IsTrue)
   136  	c.Check(auth(names.NewUserTag("bar")), jc.IsTrue)
   137  	c.Check(auth(names.NewUserTag("baz")), jc.IsTrue)
   138  	c.Check(auth(names.NewUserTag("quux")), jc.IsFalse)
   139  }
   140  
   141  func u(unit string) names.Tag { return names.NewUnitTag(unit) }
   142  
   143  func (s *commonSuite) TestAuthFuncForTagKind(c *gc.C) {
   144  	// TODO(dimitern): This list of all supported tags and kinds needs
   145  	// to live in juju/names.
   146  	uuid, err := utils.NewUUID()
   147  	c.Assert(err, jc.ErrorIsNil)
   148  
   149  	allTags := []names.Tag{
   150  		nil, // invalid tag
   151  		names.NewActionTag(uuid.String()),
   152  		names.NewCharmTag("cs:precise/missing"),
   153  		names.NewModelTag(uuid.String()),
   154  		names.NewFilesystemTag("20/20"),
   155  		names.NewLocalUserTag("user"),
   156  		names.NewMachineTag("42"),
   157  		names.NewRelationTag("wordpress:mysql mysql:db"),
   158  		names.NewApplicationTag("wordpress"),
   159  		names.NewSpaceTag("apps"),
   160  		names.NewStorageTag("foo/42"),
   161  		names.NewUnitTag("wordpress/5"),
   162  		names.NewUserTag("joe"),
   163  		names.NewVolumeTag("80/20"),
   164  	}
   165  	for i, allowedTag := range allTags {
   166  		c.Logf("test #%d: allowedTag: %v", i, allowedTag)
   167  
   168  		var allowedKind string
   169  		if allowedTag != nil {
   170  			allowedKind = allowedTag.Kind()
   171  		}
   172  		getAuthFunc := common.AuthFuncForTagKind(allowedKind)
   173  
   174  		authFunc, err := getAuthFunc()
   175  		if allowedKind == "" {
   176  			c.Check(err, gc.ErrorMatches, "tag kind cannot be empty")
   177  			c.Check(authFunc, gc.IsNil)
   178  			continue
   179  		} else if !c.Check(err, jc.ErrorIsNil) {
   180  			continue
   181  		}
   182  
   183  		for j, givenTag := range allTags {
   184  			c.Logf("test #%d.%d: givenTag: %v", i, j, givenTag)
   185  
   186  			var givenKind string
   187  			if givenTag != nil {
   188  				givenKind = givenTag.Kind()
   189  			}
   190  			if allowedKind == givenKind {
   191  				c.Check(authFunc(givenTag), jc.IsTrue)
   192  			} else {
   193  				c.Check(authFunc(givenTag), jc.IsFalse)
   194  			}
   195  		}
   196  	}
   197  }