blob: 47eccea8f136bbc201f28d5d8c708cfdcf445caf [file] [log] [blame]
Jamie Hannafordaf4570f2015-02-12 13:33:25 +01001package users
2
3import (
4 "testing"
5
6 db "github.com/rackspace/gophercloud/openstack/db/v1/databases"
7 "github.com/rackspace/gophercloud/pagination"
8 th "github.com/rackspace/gophercloud/testhelper"
9 fake "github.com/rackspace/gophercloud/testhelper/client"
Jamie Hannafordd3a78ef2015-02-18 12:17:16 +010010 "github.com/rackspace/gophercloud/testhelper/fixture"
Jamie Hannafordaf4570f2015-02-12 13:33:25 +010011)
12
Jamie Hannafordd3a78ef2015-02-18 12:17:16 +010013var (
14 instanceID = "{instanceID}"
15 _rootURL = "/instances/" + instanceID + "/users"
16)
Jamie Hannafordaf4570f2015-02-12 13:33:25 +010017
18func TestCreate(t *testing.T) {
19 th.SetupHTTP()
20 defer th.TeardownHTTP()
Jamie Hannafordd3a78ef2015-02-18 12:17:16 +010021 fixture.SetupHandler(t, _rootURL, "POST", createReq, "", 202)
Jamie Hannafordaf4570f2015-02-12 13:33:25 +010022
23 opts := BatchCreateOpts{
24 CreateOpts{
25 Databases: db.BatchCreateOpts{
26 db.CreateOpts{Name: "databaseA"},
27 },
28 Name: "dbuser3",
29 Password: "secretsecret",
30 },
31 CreateOpts{
32 Databases: db.BatchCreateOpts{
33 db.CreateOpts{Name: "databaseB"},
34 db.CreateOpts{Name: "databaseC"},
35 },
36 Name: "dbuser4",
37 Password: "secretsecret",
38 },
39 }
40
41 res := Create(fake.ServiceClient(), instanceID, opts)
42 th.AssertNoErr(t, res.Err)
43}
44
45func TestUserList(t *testing.T) {
46 th.SetupHTTP()
47 defer th.TeardownHTTP()
Jamie Hannafordd3a78ef2015-02-18 12:17:16 +010048 fixture.SetupHandler(t, _rootURL, "GET", "", listResp, 200)
Jamie Hannafordaf4570f2015-02-12 13:33:25 +010049
50 expectedUsers := []User{
51 User{
52 Databases: []db.Database{
53 db.Database{Name: "databaseA"},
54 },
55 Name: "dbuser3",
56 },
57 User{
58 Databases: []db.Database{
59 db.Database{Name: "databaseB"},
60 db.Database{Name: "databaseC"},
61 },
62 Name: "dbuser4",
63 },
64 }
65
66 pages := 0
67 err := List(fake.ServiceClient(), instanceID).EachPage(func(page pagination.Page) (bool, error) {
68 pages++
69
70 actual, err := ExtractUsers(page)
71 if err != nil {
72 return false, err
73 }
74
75 th.CheckDeepEquals(t, expectedUsers, actual)
Jamie Hannafordaf4570f2015-02-12 13:33:25 +010076 return true, nil
77 })
78
79 th.AssertNoErr(t, err)
Jamie Hannafordd3a78ef2015-02-18 12:17:16 +010080 th.AssertEquals(t, 1, pages)
Jamie Hannafordaf4570f2015-02-12 13:33:25 +010081}
82
83func TestDeleteInstance(t *testing.T) {
84 th.SetupHTTP()
85 defer th.TeardownHTTP()
Jamie Hannafordd3a78ef2015-02-18 12:17:16 +010086 fixture.SetupHandler(t, _rootURL+"/{dbName}", "DELETE", "", "", 202)
Jamie Hannafordaf4570f2015-02-12 13:33:25 +010087
88 res := Delete(fake.ServiceClient(), instanceID, "{dbName}")
89 th.AssertNoErr(t, res.Err)
90}