github.com/wallyworld/juju@v0.0.0-20161013125918-6cf1bc9d917a/mongo/admin_test.go (about) 1 // Copyright 2014 Canonical Ltd. 2 // Licensed under the AGPLv3, see LICENCE file for details. 3 4 package mongo_test 5 6 import ( 7 gitjujutesting "github.com/juju/testing" 8 jc "github.com/juju/testing/checkers" 9 gc "gopkg.in/check.v1" 10 "gopkg.in/mgo.v2" 11 "gopkg.in/mgo.v2/bson" 12 13 "github.com/juju/juju/mongo" 14 svctesting "github.com/juju/juju/service/common/testing" 15 coretesting "github.com/juju/juju/testing" 16 ) 17 18 type adminSuite struct { 19 coretesting.BaseSuite 20 21 data *svctesting.FakeServiceData 22 } 23 24 var _ = gc.Suite(&adminSuite{}) 25 26 func (s *adminSuite) SetUpTest(c *gc.C) { 27 s.BaseSuite.SetUpTest(c) 28 29 s.data = svctesting.NewFakeServiceData() 30 mongo.PatchService(s.PatchValue, s.data) 31 } 32 33 func (s *adminSuite) setUpMongo(c *gc.C) *mgo.DialInfo { 34 inst := &gitjujutesting.MgoInstance{} 35 err := inst.Start(coretesting.Certs) 36 c.Assert(err, jc.ErrorIsNil) 37 s.AddCleanup(func(*gc.C) { inst.Destroy() }) 38 dialInfo := inst.DialInfo() 39 dialInfo.Direct = true 40 return dialInfo 41 } 42 43 func checkRoles(c *gc.C, session *mgo.Session, db, user string, expected []interface{}) { 44 admin := session.DB("admin") 45 46 var info map[string]interface{} 47 err := admin.C("system.users").Find(bson.D{{"user", user}}).One(&info) 48 c.Assert(err, jc.ErrorIsNil) 49 50 var roles []interface{} 51 for _, role := range info["roles"].([]interface{}) { 52 switch role := role.(type) { 53 case map[string]interface{}: 54 // Mongo 2.6 55 if role["db"] == db { 56 roles = append(roles, role["role"]) 57 } 58 default: 59 // Mongo 2.4 60 roles = append(roles, role) 61 } 62 } 63 c.Assert(roles, jc.SameContents, expected) 64 } 65 66 func (s *adminSuite) TestSetAdminMongoPassword(c *gc.C) { 67 dialInfo := s.setUpMongo(c) 68 session, err := mgo.DialWithInfo(dialInfo) 69 c.Assert(err, jc.ErrorIsNil) 70 defer session.Close() 71 72 // Check that we can SetAdminMongoPassword to nothing when there's 73 // no password currently set. 74 err = mongo.SetAdminMongoPassword(session, "auser", "") 75 c.Assert(err, jc.ErrorIsNil) 76 77 admin := session.DB("admin") 78 err = mongo.SetAdminMongoPassword(session, "auser", "foo") 79 c.Assert(err, jc.ErrorIsNil) 80 err = admin.Login("auser", "") 81 c.Assert(err, gc.ErrorMatches, "(auth|(.*Authentication)) fail(s|ed)\\.?") 82 err = admin.Login("auser", "foo") 83 c.Assert(err, jc.ErrorIsNil) 84 85 checkRoles(c, session, "admin", "auser", 86 []interface{}{ 87 string(mgo.RoleReadWriteAny), 88 string(mgo.RoleDBAdminAny), 89 string(mgo.RoleUserAdminAny), 90 string(mgo.RoleClusterAdmin)}) 91 }