2 * Copyright 2018 Intel Corporation, Inc
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
24 smsbackend "sms/backend"
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
37 func (b *TestBackend) Init() error {
41 func (b *TestBackend) GetStatus() (bool, error) {
45 func (b *TestBackend) Unseal(shard string) error {
49 func (b *TestBackend) GetSecret(dom string, sec string) (smsbackend.Secret, error) {
50 return smsbackend.Secret{}, nil
53 func (b *TestBackend) ListSecret(dom string) ([]string, error) {
57 func (b *TestBackend) CreateSecretDomain(name string) (smsbackend.SecretDomain, error) {
58 return smsbackend.SecretDomain{}, nil
61 func (b *TestBackend) CreateSecret(dom string, sec smsbackend.Secret) error {
65 func (b *TestBackend) DeleteSecretDomain(name string) error {
69 func (b *TestBackend) DeleteSecret(dom string, name string) error {
74 testBackend := &TestBackend{}
75 h = handler{secretBackend: testBackend}
78 func TestCreateRouter(t *testing.T) {
79 router := CreateRouter(h.secretBackend)
81 t.Fatal("CreateRouter: Got error when none expected")
85 func TestStatusHandler(t *testing.T) {
86 req, err := http.NewRequest("GET", "/v1/sms/status", nil)
91 rr := httptest.NewRecorder()
92 hr := http.HandlerFunc(h.statusHandler)
97 if ret != http.StatusOK {
98 t.Errorf("statusHandler returned wrong status code: %v vs %v",
102 expected := backendStatus{}
103 got := backendStatus{}
104 expectedStr := strings.NewReader(`{"sealstatus":true}`)
105 json.NewDecoder(expectedStr).Decode(&expected)
106 json.NewDecoder(rr.Body).Decode(&got)
108 if reflect.DeepEqual(expected, got) == false {
109 t.Errorf("statusHandler returned unexpected body: got %v vs %v",
110 rr.Body.String(), expectedStr)