Update listsecret return to send a JSON object
[aaf/sms.git] / sms-service / src / sms / handler / handler_test.go
1 /*
2  * Copyright 2018 Intel Corporation, Inc
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 package handler
18
19 import (
20         "bytes"
21         "encoding/json"
22         "net/http"
23         "net/http/httptest"
24         "reflect"
25         smsbackend "sms/backend"
26         "strings"
27         "testing"
28 )
29
30 var h handler
31
32 // Here we are using the anonymous variable feature of golang to
33 // override methods form an interface
34 type TestBackend struct {
35         smsbackend.SecretBackend
36 }
37
38 func (b *TestBackend) Init() error {
39         return nil
40 }
41
42 func (b *TestBackend) GetStatus() (bool, error) {
43         return true, nil
44 }
45
46 func (b *TestBackend) Unseal(shard string) error {
47         return nil
48 }
49
50 func (b *TestBackend) GetSecret(dom string, sec string) (smsbackend.Secret, error) {
51         return smsbackend.Secret{
52                 Name: "testsecret",
53                 Values: map[string]interface{}{
54                         "name":       "john",
55                         "profession": "engineer",
56                 },
57         }, nil
58 }
59
60 func (b *TestBackend) ListSecret(dom string) ([]string, error) {
61         return []string{"testsecret1", "testsecret2"}, nil
62 }
63
64 func (b *TestBackend) CreateSecretDomain(name string) (smsbackend.SecretDomain, error) {
65         return smsbackend.SecretDomain{UUID: "123e4567-e89b-12d3-a456-426655440000",
66                 Name: "testdomain"}, nil
67 }
68
69 func (b *TestBackend) CreateSecret(dom string, sec smsbackend.Secret) error {
70         return nil
71 }
72
73 func (b *TestBackend) DeleteSecretDomain(name string) error {
74         return nil
75 }
76
77 func (b *TestBackend) DeleteSecret(dom string, name string) error {
78         return nil
79 }
80
81 func init() {
82         testBackend := &TestBackend{}
83         h = handler{secretBackend: testBackend}
84 }
85
86 func TestCreateRouter(t *testing.T) {
87         router := CreateRouter(h.secretBackend)
88         if router == nil {
89                 t.Fatal("CreateRouter: Got error when none expected")
90         }
91 }
92
93 func TestStatusHandler(t *testing.T) {
94         req, err := http.NewRequest("GET", "/v1/sms/status", nil)
95         if err != nil {
96                 t.Fatal(err)
97         }
98
99         rr := httptest.NewRecorder()
100         hr := http.HandlerFunc(h.statusHandler)
101
102         hr.ServeHTTP(rr, req)
103
104         ret := rr.Code
105         if ret != http.StatusOK {
106                 t.Errorf("statusHandler returned wrong status code: %v vs %v",
107                         ret, http.StatusOK)
108         }
109
110         expected := backendStatus{}
111         got := backendStatus{}
112         expectedStr := strings.NewReader(`{"sealstatus":true}`)
113         json.NewDecoder(expectedStr).Decode(&expected)
114         json.NewDecoder(rr.Body).Decode(&got)
115
116         if reflect.DeepEqual(expected, got) == false {
117                 t.Errorf("statusHandler returned unexpected body: got %v vs %v",
118                         rr.Body.String(), expectedStr)
119         }
120 }
121
122 func TestCreateSecretDomainHandler(t *testing.T) {
123         body := `{"uuid":"123e4567-e89b-12d3-a456-426655440000","name":"testdomain"}`
124         reader := strings.NewReader(body)
125         req, err := http.NewRequest("POST", "/v1/sms/domain", reader)
126         if err != nil {
127                 t.Fatal(err)
128         }
129
130         rr := httptest.NewRecorder()
131         hr := http.HandlerFunc(h.createSecretDomainHandler)
132
133         hr.ServeHTTP(rr, req)
134         if rr.Code != http.StatusCreated {
135                 t.Errorf("Expected statusCreated return code. Got: %v", rr.Code)
136         }
137
138         expected := smsbackend.SecretDomain{
139                 UUID: "123e4567-e89b-12d3-a456-426655440000",
140                 Name: "testdomain",
141         }
142
143         got := smsbackend.SecretDomain{}
144         json.NewDecoder(rr.Body).Decode(&got)
145
146         if reflect.DeepEqual(expected, got) == false {
147                 t.Errorf("CreateSecretDomainHandler returned unexpected body: got %v;"+
148                         " expected %v", got, expected)
149         }
150 }
151
152 func TestCreateSecretHandler(t *testing.T) {
153         data := smsbackend.Secret{
154                 Name: "testsecret",
155                 Values: map[string]interface{}{
156                         "name":    "john",
157                         "age":     43,
158                         "isadmin": true,
159                 },
160         }
161
162         jdata, err := json.Marshal(data)
163         req, err := http.NewRequest("POST", "/v1/sms/domain/testdomain/secret", bytes.NewReader(jdata))
164         if err != nil {
165                 t.Fatal(err)
166         }
167
168         rr := httptest.NewRecorder()
169         hr := http.HandlerFunc(h.createSecretHandler)
170
171         hr.ServeHTTP(rr, req)
172         if rr.Code != http.StatusCreated {
173                 t.Errorf("Expected statusCreated return code. Got: %v", rr.Code)
174         }
175 }
176
177 func TestDeleteSecretDomainHandler(t *testing.T) {
178         req, err := http.NewRequest("DELETE", "/v1/sms/domain/testdomain", nil)
179         if err != nil {
180                 t.Fatal(err)
181         }
182
183         rr := httptest.NewRecorder()
184         hr := http.HandlerFunc(h.deleteSecretDomainHandler)
185
186         hr.ServeHTTP(rr, req)
187         if rr.Code != http.StatusNoContent {
188                 t.Errorf("Expected statusCreated return code. Got: %v", rr.Code)
189         }
190 }
191
192 func TestDeleteSecretHandler(t *testing.T) {
193         req, err := http.NewRequest("DELETE", "/v1/sms/domain/testdomain/secret/testsecret", nil)
194         if err != nil {
195                 t.Fatal(err)
196         }
197
198         rr := httptest.NewRecorder()
199         hr := http.HandlerFunc(h.deleteSecretHandler)
200
201         hr.ServeHTTP(rr, req)
202         if rr.Code != http.StatusOK {
203                 t.Errorf("Expected statusCreated return code. Got: %v", rr.Code)
204         }
205 }
206
207 func TestGetSecretHandler(t *testing.T) {
208         req, err := http.NewRequest("DELETE", "/v1/sms/domain/testdomain/secret/testsecret", nil)
209         if err != nil {
210                 t.Fatal(err)
211         }
212
213         rr := httptest.NewRecorder()
214         hr := http.HandlerFunc(h.getSecretHandler)
215
216         hr.ServeHTTP(rr, req)
217         if rr.Code != http.StatusOK {
218                 t.Errorf("Expected statusCreated return code. Got: %v", rr.Code)
219         }
220
221         expected := smsbackend.Secret{
222                 Name: "testsecret",
223                 Values: map[string]interface{}{
224                         "profession": "engineer",
225                         "name":       "john",
226                 },
227         }
228
229         got := smsbackend.Secret{}
230         json.NewDecoder(rr.Body).Decode(&got)
231
232         if reflect.DeepEqual(expected, got) == false {
233                 t.Errorf("CreateSecretDomainHandler returned unexpected body: got: %v"+
234                         " expected: %v", got, expected)
235         }
236 }
237
238 func TestListSecretHandler(t *testing.T) {
239         req, err := http.NewRequest("DELETE", "/v1/sms/domain/testdomain/secret", nil)
240         if err != nil {
241                 t.Fatal(err)
242         }
243
244         rr := httptest.NewRecorder()
245         hr := http.HandlerFunc(h.listSecretHandler)
246
247         hr.ServeHTTP(rr, req)
248         if rr.Code != http.StatusOK {
249                 t.Errorf("Expected statusCreated return code. Got: %v", rr.Code)
250         }
251
252         var expected = struct {
253                 SecretNames []string `json:"secretnames"`
254         }{
255                 []string{"testsecret1", "testsecret2"},
256         }
257
258         var got struct {
259                 SecretNames []string `json:"secretnames"`
260         }
261
262         json.NewDecoder(rr.Body).Decode(&got)
263
264         if reflect.DeepEqual(expected, got) == false {
265                 t.Errorf("CreateSecretDomainHandler returned unexpected body: got: %v"+
266                         " expected: %v", got, expected)
267         }
268 }