Samuel A. Falvo II | 8a549ef | 2014-01-24 15:20:54 -0800 | [diff] [blame] | 1 | package identity |
| 2 | |
| 3 | import ( |
| 4 | "encoding/json" |
| 5 | "testing" |
| 6 | ) |
| 7 | |
Samuel A. Falvo II | 8a549ef | 2014-01-24 15:20:54 -0800 | [diff] [blame] | 8 | func TestIsExtensionAvailable(t *testing.T) { |
| 9 | // Make a response as we'd expect from the IdentityService.GetExtensions() call. |
| 10 | getExtensionsResults := make(map[string]interface{}) |
| 11 | err := json.Unmarshal([]byte(queryResults), &getExtensionsResults) |
| 12 | if err != nil { |
| 13 | t.Error(err) |
| 14 | return |
| 15 | } |
| 16 | |
Samuel A. Falvo II | 2b96321 | 2014-02-09 02:12:30 -0800 | [diff] [blame] | 17 | e := ExtensionsResult(getExtensionsResults) |
Jon Perritt | a7099c6 | 2014-08-10 21:47:23 -0500 | [diff] [blame] | 18 | for _, alias := range []string{"OS-KSADM", "OS-FEDERATION"} { |
Samuel A. Falvo II | 8a549ef | 2014-01-24 15:20:54 -0800 | [diff] [blame] | 19 | if !e.IsExtensionAvailable(alias) { |
| 20 | t.Errorf("Expected extension %s present.", alias) |
| 21 | return |
| 22 | } |
| 23 | } |
| 24 | if e.IsExtensionAvailable("blort") { |
| 25 | t.Errorf("Input JSON doesn't list blort as an extension") |
| 26 | return |
| 27 | } |
| 28 | } |
| 29 | |
| 30 | func TestGetExtensionDetails(t *testing.T) { |
| 31 | // Make a response as we'd expect from the IdentityService.GetExtensions() call. |
| 32 | getExtensionsResults := make(map[string]interface{}) |
| 33 | err := json.Unmarshal([]byte(queryResults), &getExtensionsResults) |
| 34 | if err != nil { |
| 35 | t.Error(err) |
| 36 | return |
| 37 | } |
| 38 | |
Samuel A. Falvo II | 2b96321 | 2014-02-09 02:12:30 -0800 | [diff] [blame] | 39 | e := ExtensionsResult(getExtensionsResults) |
Jon Perritt | a7099c6 | 2014-08-10 21:47:23 -0500 | [diff] [blame] | 40 | ed, err := e.ExtensionDetailsByAlias("OS-KSADM") |
Samuel A. Falvo II | 8a549ef | 2014-01-24 15:20:54 -0800 | [diff] [blame] | 41 | if err != nil { |
| 42 | t.Error(err) |
| 43 | return |
| 44 | } |
| 45 | |
| 46 | actuals := map[string]string{ |
| 47 | "name": ed.Name, |
| 48 | "namespace": ed.Namespace, |
| 49 | "updated": ed.Updated, |
| 50 | "description": ed.Description, |
| 51 | } |
| 52 | |
| 53 | expecteds := map[string]string{ |
Jon Perritt | a7099c6 | 2014-08-10 21:47:23 -0500 | [diff] [blame] | 54 | "name": "OpenStack Keystone Admin", |
| 55 | "namespace": "http://docs.openstack.org/identity/api/ext/OS-KSADM/v1.0", |
| 56 | "updated": "2013-07-11T17:14:00-00:00", |
| 57 | "description": "OpenStack extensions to Keystone v2.0 API enabling Administrative Operations.", |
Samuel A. Falvo II | 8a549ef | 2014-01-24 15:20:54 -0800 | [diff] [blame] | 58 | } |
| 59 | |
| 60 | for k, v := range expecteds { |
| 61 | if actuals[k] != v { |
| 62 | t.Errorf("Expected %s \"%s\", got \"%s\" instead", k, v, actuals[k]) |
| 63 | return |
| 64 | } |
| 65 | } |
| 66 | } |
Samuel A. Falvo II | 2b96321 | 2014-02-09 02:12:30 -0800 | [diff] [blame] | 67 | |
| 68 | func TestMalformedResponses(t *testing.T) { |
| 69 | getExtensionsResults := make(map[string]interface{}) |
| 70 | err := json.Unmarshal([]byte(bogusExtensionsResults), &getExtensionsResults) |
| 71 | if err != nil { |
| 72 | t.Error(err) |
| 73 | return |
| 74 | } |
| 75 | e := ExtensionsResult(getExtensionsResults) |
| 76 | |
Jon Perritt | a7099c6 | 2014-08-10 21:47:23 -0500 | [diff] [blame] | 77 | _, err = e.ExtensionDetailsByAlias("OS-KSADM") |
Samuel A. Falvo II | 2b96321 | 2014-02-09 02:12:30 -0800 | [diff] [blame] | 78 | if err == nil { |
| 79 | t.Error("Expected ErrNotImplemented at least") |
| 80 | return |
| 81 | } |
| 82 | if err != ErrNotImplemented { |
| 83 | t.Error("Expected ErrNotImplemented") |
| 84 | return |
| 85 | } |
| 86 | |
| 87 | if e.IsExtensionAvailable("anything at all") { |
| 88 | t.Error("No extensions are available with a bogus result.") |
| 89 | return |
| 90 | } |
| 91 | } |
| 92 | |
| 93 | func TestAliases(t *testing.T) { |
| 94 | getExtensionsResults := make(map[string]interface{}) |
| 95 | err := json.Unmarshal([]byte(queryResults), &getExtensionsResults) |
| 96 | if err != nil { |
| 97 | t.Error(err) |
| 98 | return |
| 99 | } |
| 100 | |
| 101 | e := ExtensionsResult(getExtensionsResults) |
| 102 | aliases, err := e.Aliases() |
| 103 | if err != nil { |
| 104 | t.Error(err) |
| 105 | return |
| 106 | } |
Jon Perritt | a7099c6 | 2014-08-10 21:47:23 -0500 | [diff] [blame] | 107 | extensions := (((e["extensions"]).(map[string]interface{}))["values"]).([]interface{}) |
| 108 | if len(aliases) != len(extensions) { |
Samuel A. Falvo II | 2b96321 | 2014-02-09 02:12:30 -0800 | [diff] [blame] | 109 | t.Error("Expected one alias name per extension") |
| 110 | return |
| 111 | } |
| 112 | } |