blob: 3000fc0137ad313f608bbd80f5f3cbd34fe095f4 [file] [log] [blame]
Samuel A. Falvo II8a549ef2014-01-24 15:20:54 -08001package identity
2
3import (
4 "encoding/json"
5 "testing"
6)
7
Samuel A. Falvo II8a549ef2014-01-24 15:20:54 -08008func 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 II2b963212014-02-09 02:12:30 -080017 e := ExtensionsResult(getExtensionsResults)
Jon Perritta7099c62014-08-10 21:47:23 -050018 for _, alias := range []string{"OS-KSADM", "OS-FEDERATION"} {
Samuel A. Falvo II8a549ef2014-01-24 15:20:54 -080019 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
30func 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 II2b963212014-02-09 02:12:30 -080039 e := ExtensionsResult(getExtensionsResults)
Jon Perritta7099c62014-08-10 21:47:23 -050040 ed, err := e.ExtensionDetailsByAlias("OS-KSADM")
Samuel A. Falvo II8a549ef2014-01-24 15:20:54 -080041 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 Perritta7099c62014-08-10 21:47:23 -050054 "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 II8a549ef2014-01-24 15:20:54 -080058 }
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 II2b963212014-02-09 02:12:30 -080067
68func 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 Perritta7099c62014-08-10 21:47:23 -050077 _, err = e.ExtensionDetailsByAlias("OS-KSADM")
Samuel A. Falvo II2b963212014-02-09 02:12:30 -080078 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
93func 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 Perritta7099c62014-08-10 21:47:23 -0500107 extensions := (((e["extensions"]).(map[string]interface{}))["values"]).([]interface{})
108 if len(aliases) != len(extensions) {
Samuel A. Falvo II2b963212014-02-09 02:12:30 -0800109 t.Error("Expected one alias name per extension")
110 return
111 }
112}