github.com/altoros/juju-vmware@v0.0.0-20150312064031-f19ae857ccca/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 gc "gopkg.in/check.v1" 12 13 "github.com/juju/juju/apiserver/common" 14 coretesting "github.com/juju/juju/testing" 15 "github.com/juju/names" 16 ) 17 18 func TestAll(t *stdtesting.T) { 19 coretesting.MgoTestPackage(t) 20 } 21 22 type commonSuite struct{} 23 24 var _ = gc.Suite(&commonSuite{}) 25 26 func errorAuth() (common.AuthFunc, error) { 27 return nil, fmt.Errorf("pow") 28 } 29 30 func fooAuth() (common.AuthFunc, error) { 31 return func(tag names.Tag) bool { 32 return tag == names.NewUserTag("foo") 33 }, nil 34 } 35 36 func barAuth() (common.AuthFunc, error) { 37 return func(tag names.Tag) bool { 38 return tag == names.NewUserTag("bar") 39 }, nil 40 } 41 42 var authEitherTests = []struct { 43 about string 44 a, b func() (common.AuthFunc, error) 45 tag names.Tag 46 expect bool 47 err string 48 }{{ 49 about: "a returns an error", 50 a: errorAuth, 51 b: fooAuth, 52 err: "pow", 53 }, { 54 about: "b returns an error", 55 a: fooAuth, 56 b: errorAuth, 57 err: "pow", 58 }, { 59 about: "both a and b return an error", 60 a: errorAuth, 61 b: errorAuth, 62 err: "pow", 63 }, { 64 about: "tag foo - a returns true", 65 a: fooAuth, 66 b: barAuth, 67 tag: names.NewUserTag("foo"), 68 expect: true, 69 }, { 70 about: "tag foo - b returns true", 71 a: barAuth, 72 b: fooAuth, 73 tag: names.NewUserTag("foo"), 74 expect: true, 75 }, { 76 about: "tag bar - b returns true", 77 a: fooAuth, 78 b: barAuth, 79 tag: names.NewUserTag("bar"), 80 expect: true, 81 }, { 82 about: "tag foo - both return true", 83 a: fooAuth, 84 b: fooAuth, 85 tag: names.NewUserTag("foo"), 86 expect: true, 87 }, { 88 about: "tag baz - both return false", 89 a: fooAuth, 90 b: barAuth, 91 tag: names.NewUserTag("baz"), 92 expect: false, 93 }, { 94 about: "tag quxx - both return false", 95 a: fooAuth, 96 b: barAuth, 97 tag: names.NewServiceTag("quxx"), 98 expect: false, 99 }} 100 101 func (s *commonSuite) TestAuthEither(c *gc.C) { 102 for i, test := range authEitherTests { 103 c.Logf("test %d: %s", i, test.about) 104 authEither := common.AuthEither(test.a, test.b) 105 either, err := authEither() 106 if test.err == "" { 107 c.Assert(err, jc.ErrorIsNil) 108 ok := either(test.tag) 109 c.Assert(ok, gc.Equals, test.expect) 110 } else { 111 c.Assert(err, gc.ErrorMatches, test.err) 112 c.Assert(either, gc.IsNil) 113 } 114 } 115 } 116 117 func u(unit string) names.Tag { return names.NewUnitTag(unit) } 118 func m(machine string) names.Tag { return names.NewMachineTag(machine) }