Ash Wilson | 9d085a8 | 2014-10-03 13:05:03 -0400 | [diff] [blame^] | 1 | package tenants |
| 2 | |
| 3 | import ( |
| 4 | "fmt" |
| 5 | "net/http" |
| 6 | "testing" |
| 7 | |
| 8 | "github.com/rackspace/gophercloud" |
| 9 | "github.com/rackspace/gophercloud/pagination" |
| 10 | th "github.com/rackspace/gophercloud/testhelper" |
| 11 | ) |
| 12 | |
| 13 | const tokenID = "1234123412341234" |
| 14 | |
| 15 | func TestListTenants(t *testing.T) { |
| 16 | th.SetupHTTP() |
| 17 | defer th.TeardownHTTP() |
| 18 | |
| 19 | th.Mux.HandleFunc("/tenants", func(w http.ResponseWriter, r *http.Request) { |
| 20 | th.TestMethod(t, r, "GET") |
| 21 | th.TestHeader(t, r, "Accept", "application/json") |
| 22 | th.TestHeader(t, r, "X-Auth-Token", tokenID) |
| 23 | |
| 24 | w.Header().Set("Content-Type", "application/json") |
| 25 | w.WriteHeader(http.StatusOK) |
| 26 | fmt.Fprintf(w, ` |
| 27 | { |
| 28 | "tenants": [ |
| 29 | { |
| 30 | "id": "1234", |
| 31 | "name": "Red Team", |
| 32 | "description": "The team that is red", |
| 33 | "enabled": true |
| 34 | }, |
| 35 | { |
| 36 | "id": "9876", |
| 37 | "name": "Blue Team", |
| 38 | "description": "The team that is blue", |
| 39 | "enabled": false |
| 40 | } |
| 41 | ] |
| 42 | } |
| 43 | `) |
| 44 | }) |
| 45 | |
| 46 | client := &gophercloud.ServiceClient{ |
| 47 | Provider: &gophercloud.ProviderClient{TokenID: tokenID}, |
| 48 | Endpoint: th.Endpoint(), |
| 49 | } |
| 50 | |
| 51 | count := 0 |
| 52 | err := List(client, nil).EachPage(func(page pagination.Page) (bool, error) { |
| 53 | count++ |
| 54 | |
| 55 | actual, err := ExtractTenants(page) |
| 56 | th.AssertNoErr(t, err) |
| 57 | |
| 58 | expected := []Tenant{ |
| 59 | Tenant{ |
| 60 | ID: "1234", |
| 61 | Name: "Red Team", |
| 62 | Description: "The team that is red", |
| 63 | Enabled: true, |
| 64 | }, |
| 65 | Tenant{ |
| 66 | ID: "9876", |
| 67 | Name: "Blue Team", |
| 68 | Description: "The team that is blue", |
| 69 | Enabled: false, |
| 70 | }, |
| 71 | } |
| 72 | |
| 73 | th.CheckDeepEquals(t, expected, actual) |
| 74 | |
| 75 | return true, nil |
| 76 | }) |
| 77 | th.AssertNoErr(t, err) |
| 78 | th.CheckEquals(t, count, 1) |
| 79 | } |