Jamie Hannaford | 3c1796b | 2015-02-12 14:21:33 +0100 | [diff] [blame^] | 1 | package users |
| 2 | |
| 3 | import ( |
| 4 | "testing" |
| 5 | |
| 6 | db "github.com/rackspace/gophercloud/openstack/db/v1/databases" |
| 7 | os "github.com/rackspace/gophercloud/openstack/db/v1/users" |
| 8 | "github.com/rackspace/gophercloud/pagination" |
| 9 | th "github.com/rackspace/gophercloud/testhelper" |
| 10 | fake "github.com/rackspace/gophercloud/testhelper/client" |
| 11 | ) |
| 12 | |
| 13 | const instanceID = "{instanceID}" |
| 14 | |
| 15 | func TestCreate(t *testing.T) { |
| 16 | th.SetupHTTP() |
| 17 | defer th.TeardownHTTP() |
| 18 | |
| 19 | os.HandleCreateUserSuccessfully(t, instanceID) |
| 20 | |
| 21 | opts := os.BatchCreateOpts{ |
| 22 | os.CreateOpts{ |
| 23 | Databases: db.BatchCreateOpts{ |
| 24 | db.CreateOpts{Name: "databaseA"}, |
| 25 | }, |
| 26 | Name: "dbuser3", |
| 27 | Password: "secretsecret", |
| 28 | }, |
| 29 | os.CreateOpts{ |
| 30 | Databases: db.BatchCreateOpts{ |
| 31 | db.CreateOpts{Name: "databaseB"}, |
| 32 | db.CreateOpts{Name: "databaseC"}, |
| 33 | }, |
| 34 | Name: "dbuser4", |
| 35 | Password: "secretsecret", |
| 36 | }, |
| 37 | } |
| 38 | |
| 39 | res := Create(fake.ServiceClient(), instanceID, opts) |
| 40 | th.AssertNoErr(t, res.Err) |
| 41 | } |
| 42 | |
| 43 | func TestUserList(t *testing.T) { |
| 44 | th.SetupHTTP() |
| 45 | defer th.TeardownHTTP() |
| 46 | |
| 47 | os.HandleListUsersSuccessfully(t, instanceID) |
| 48 | |
| 49 | expectedUsers := []os.User{ |
| 50 | os.User{ |
| 51 | Databases: []db.Database{ |
| 52 | db.Database{Name: "databaseA"}, |
| 53 | }, |
| 54 | Name: "dbuser3", |
| 55 | }, |
| 56 | os.User{ |
| 57 | Databases: []db.Database{ |
| 58 | db.Database{Name: "databaseB"}, |
| 59 | db.Database{Name: "databaseC"}, |
| 60 | }, |
| 61 | Name: "dbuser4", |
| 62 | }, |
| 63 | } |
| 64 | |
| 65 | pages := 0 |
| 66 | err := List(fake.ServiceClient(), instanceID).EachPage(func(page pagination.Page) (bool, error) { |
| 67 | pages++ |
| 68 | |
| 69 | actual, err := os.ExtractUsers(page) |
| 70 | if err != nil { |
| 71 | return false, err |
| 72 | } |
| 73 | |
| 74 | th.CheckDeepEquals(t, expectedUsers, actual) |
| 75 | |
| 76 | return true, nil |
| 77 | }) |
| 78 | |
| 79 | th.AssertNoErr(t, err) |
| 80 | |
| 81 | if pages != 1 { |
| 82 | t.Errorf("Expected 1 page, saw %d", pages) |
| 83 | } |
| 84 | } |
| 85 | |
| 86 | func TestDeleteInstance(t *testing.T) { |
| 87 | th.SetupHTTP() |
| 88 | defer th.TeardownHTTP() |
| 89 | |
| 90 | os.HandleDeleteUserSuccessfully(t, instanceID, "{userName}") |
| 91 | |
| 92 | res := Delete(fake.ServiceClient(), instanceID, "{userName}") |
| 93 | th.AssertNoErr(t, res.Err) |
| 94 | } |