Init role does not depend on vault state
[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         "encoding/json"
21         "net/http"
22         "net/http/httptest"
23         "reflect"
24         smsbackend "sms/backend"
25         "strings"
26         "testing"
27 )
28
29 var h handler
30
31 // Here we are using the anonymous variable feature of golang to
32 // override methods form an interface
33 type TestBackend struct {
34         smsbackend.SecretBackend
35 }
36
37 func (b *TestBackend) Init() error {
38         return nil
39 }
40
41 func (b *TestBackend) GetStatus() (bool, error) {
42         return true, nil
43 }
44
45 func (b *TestBackend) GetSecret(dom string, sec string) (smsbackend.Secret, error) {
46         return smsbackend.Secret{}, nil
47 }
48
49 func (b *TestBackend) ListSecret(dom string) ([]string, error) {
50         return nil, nil
51 }
52
53 func (b *TestBackend) CreateSecretDomain(name string) (smsbackend.SecretDomain, error) {
54         return smsbackend.SecretDomain{}, nil
55 }
56
57 func (b *TestBackend) CreateSecret(dom string, sec smsbackend.Secret) error {
58         return nil
59 }
60
61 func (b *TestBackend) DeleteSecretDomain(name string) error {
62         return nil
63 }
64
65 func (b *TestBackend) DeleteSecret(dom string, name string) error {
66         return nil
67 }
68
69 func init() {
70         testBackend := &TestBackend{}
71         h = handler{secretBackend: testBackend}
72 }
73
74 func TestCreateRouter(t *testing.T) {
75         router := CreateRouter(h.secretBackend)
76         if router == nil {
77                 t.Fatal("CreateRouter: Got error when none expected")
78         }
79 }
80
81 func TestStatusHandler(t *testing.T) {
82         req, err := http.NewRequest("GET", "/v1/sms/status", nil)
83         if err != nil {
84                 t.Fatal(err)
85         }
86
87         rr := httptest.NewRecorder()
88         hr := http.HandlerFunc(h.statusHandler)
89
90         hr.ServeHTTP(rr, req)
91
92         ret := rr.Code
93         if ret != http.StatusOK {
94                 t.Errorf("statusHandler returned wrong status code: %v vs %v",
95                         ret, http.StatusOK)
96         }
97
98         expected := backendStatus{}
99         got := backendStatus{}
100         expectedStr := strings.NewReader(`{"sealstatus":true}`)
101         json.NewDecoder(expectedStr).Decode(&expected)
102         json.NewDecoder(rr.Body).Decode(&got)
103
104         if reflect.DeepEqual(expected, got) == false {
105                 t.Errorf("statusHandler returned unexpected body: got %v vs %v",
106                         rr.Body.String(), expectedStr)
107         }
108 }