github.com/axw/juju@v0.0.0-20161005053422-4bd6544d08d4/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  var authEitherTests = []struct {
    44  	about  string
    45  	a, b   func() (common.AuthFunc, error)
    46  	tag    names.Tag
    47  	expect bool
    48  	err    string
    49  }{{
    50  	about: "a returns an error",
    51  	a:     errorAuth,
    52  	b:     fooAuth,
    53  	err:   "pow",
    54  }, {
    55  	about: "b returns an error",
    56  	a:     fooAuth,
    57  	b:     errorAuth,
    58  	err:   "pow",
    59  }, {
    60  	about: "both a and b return an error",
    61  	a:     errorAuth,
    62  	b:     errorAuth,
    63  	err:   "pow",
    64  }, {
    65  	about:  "tag foo - a returns true",
    66  	a:      fooAuth,
    67  	b:      barAuth,
    68  	tag:    names.NewUserTag("foo"),
    69  	expect: true,
    70  }, {
    71  	about:  "tag foo - b returns true",
    72  	a:      barAuth,
    73  	b:      fooAuth,
    74  	tag:    names.NewUserTag("foo"),
    75  	expect: true,
    76  }, {
    77  	about:  "tag bar - b returns true",
    78  	a:      fooAuth,
    79  	b:      barAuth,
    80  	tag:    names.NewUserTag("bar"),
    81  	expect: true,
    82  }, {
    83  	about:  "tag foo - both return true",
    84  	a:      fooAuth,
    85  	b:      fooAuth,
    86  	tag:    names.NewUserTag("foo"),
    87  	expect: true,
    88  }, {
    89  	about:  "tag baz - both return false",
    90  	a:      fooAuth,
    91  	b:      barAuth,
    92  	tag:    names.NewUserTag("baz"),
    93  	expect: false,
    94  }, {
    95  	about:  "tag quxx - both return false",
    96  	a:      fooAuth,
    97  	b:      barAuth,
    98  	tag:    names.NewApplicationTag("quxx"),
    99  	expect: false,
   100  }}
   101  
   102  func (s *commonSuite) TestAuthEither(c *gc.C) {
   103  	for i, test := range authEitherTests {
   104  		c.Logf("test %d: %s", i, test.about)
   105  		authEither := common.AuthEither(test.a, test.b)
   106  		either, err := authEither()
   107  		if test.err == "" {
   108  			c.Assert(err, jc.ErrorIsNil)
   109  			ok := either(test.tag)
   110  			c.Assert(ok, gc.Equals, test.expect)
   111  		} else {
   112  			c.Assert(err, gc.ErrorMatches, test.err)
   113  			c.Assert(either, gc.IsNil)
   114  		}
   115  	}
   116  }
   117  
   118  func u(unit string) names.Tag                 { return names.NewUnitTag(unit) }
   119  func ApplicationTag(service string) names.Tag { return names.NewApplicationTag(service) }
   120  func m(machine string) names.Tag              { return names.NewMachineTag(machine) }
   121  
   122  func (s *commonSuite) TestAuthFuncForTagKind(c *gc.C) {
   123  	// TODO(dimitern): This list of all supported tags and kinds needs
   124  	// to live in juju/names.
   125  	uuid, err := utils.NewUUID()
   126  	c.Assert(err, jc.ErrorIsNil)
   127  
   128  	allTags := []names.Tag{
   129  		nil, // invalid tag
   130  		names.NewActionTag(uuid.String()),
   131  		names.NewCharmTag("cs:precise/missing"),
   132  		names.NewModelTag(uuid.String()),
   133  		names.NewFilesystemTag("20/20"),
   134  		names.NewLocalUserTag("user"),
   135  		names.NewMachineTag("42"),
   136  		names.NewRelationTag("wordpress:mysql mysql:db"),
   137  		names.NewApplicationTag("wordpress"),
   138  		names.NewSpaceTag("apps"),
   139  		names.NewStorageTag("foo/42"),
   140  		names.NewUnitTag("wordpress/5"),
   141  		names.NewUserTag("joe"),
   142  		names.NewVolumeTag("80/20"),
   143  	}
   144  	for i, allowedTag := range allTags {
   145  		c.Logf("test #%d: allowedTag: %v", i, allowedTag)
   146  
   147  		var allowedKind string
   148  		if allowedTag != nil {
   149  			allowedKind = allowedTag.Kind()
   150  		}
   151  		getAuthFunc := common.AuthFuncForTagKind(allowedKind)
   152  
   153  		authFunc, err := getAuthFunc()
   154  		if allowedKind == "" {
   155  			c.Check(err, gc.ErrorMatches, "tag kind cannot be empty")
   156  			c.Check(authFunc, gc.IsNil)
   157  			continue
   158  		} else if !c.Check(err, jc.ErrorIsNil) {
   159  			continue
   160  		}
   161  
   162  		for j, givenTag := range allTags {
   163  			c.Logf("test #%d.%d: givenTag: %v", i, j, givenTag)
   164  
   165  			var givenKind string
   166  			if givenTag != nil {
   167  				givenKind = givenTag.Kind()
   168  			}
   169  			if allowedKind == givenKind {
   170  				c.Check(authFunc(givenTag), jc.IsTrue)
   171  			} else {
   172  				c.Check(authFunc(givenTag), jc.IsFalse)
   173  			}
   174  		}
   175  	}
   176  }