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