diff options
author | Kiran Kamineni <kiran.k.kamineni@intel.com> | 2018-03-08 20:58:29 -0800 |
---|---|---|
committer | Kiran Kamineni <kiran.k.kamineni@intel.com> | 2018-03-08 20:58:38 -0800 |
commit | 0b3385c4fc8e728a2bff17ef7682c6e75918c2f2 (patch) | |
tree | 4832a5b65f2240f69b641badf3dbcd0327139382 | |
parent | 8c18c6463962cffd5eec869f7fa2aff6a6fdbec6 (diff) |
Adding handler unit tests
Adding updated unit tests for handler.go
They needed to be fleshed out based on the handler implementation
Issue-ID: AAF-131
Change-Id: Id161faef7500947d858e99b9648f951bc1e54e09
Signed-off-by: Kiran Kamineni <kiran.k.kamineni@intel.com>
-rw-r--r-- | sms-service/src/sms/Gopkg.lock | 2 | ||||
-rw-r--r-- | sms-service/src/sms/handler/handler_test.go | 155 |
2 files changed, 153 insertions, 4 deletions
diff --git a/sms-service/src/sms/Gopkg.lock b/sms-service/src/sms/Gopkg.lock index 02e2de6..12bdfab 100644 --- a/sms-service/src/sms/Gopkg.lock +++ b/sms-service/src/sms/Gopkg.lock @@ -156,6 +156,6 @@ [solve-meta] analyzer-name = "dep" analyzer-version = 1 - inputs-digest = "7308c3e7f4b4c4536cbae1a89e132c8512650127871bb669a679d81f4563ed92" + inputs-digest = "c3b1a2cc60523cdaccc247048d5e99d9b2f5c5f5e7f66c3e1deb9e709ec6f8bc" solver-name = "gps-cdcl" solver-version = 1 diff --git a/sms-service/src/sms/handler/handler_test.go b/sms-service/src/sms/handler/handler_test.go index 82bd78e..e1b5b34 100644 --- a/sms-service/src/sms/handler/handler_test.go +++ b/sms-service/src/sms/handler/handler_test.go @@ -17,6 +17,7 @@ package handler import ( + "bytes" "encoding/json" "net/http" "net/http/httptest" @@ -47,15 +48,22 @@ func (b *TestBackend) Unseal(shard string) error { } func (b *TestBackend) GetSecret(dom string, sec string) (smsbackend.Secret, error) { - return smsbackend.Secret{}, nil + return smsbackend.Secret{ + Name: "testsecret", + Values: map[string]interface{}{ + "name": "john", + "profession": "engineer", + }, + }, nil } func (b *TestBackend) ListSecret(dom string) ([]string, error) { - return nil, nil + return []string{"testsecret1", "testsecret2"}, nil } func (b *TestBackend) CreateSecretDomain(name string) (smsbackend.SecretDomain, error) { - return smsbackend.SecretDomain{}, nil + return smsbackend.SecretDomain{UUID: "123e4567-e89b-12d3-a456-426655440000", + Name: "testdomain"}, nil } func (b *TestBackend) CreateSecret(dom string, sec smsbackend.Secret) error { @@ -110,3 +118,144 @@ func TestStatusHandler(t *testing.T) { rr.Body.String(), expectedStr) } } + +func TestCreateSecretDomainHandler(t *testing.T) { + body := `{"uuid":"123e4567-e89b-12d3-a456-426655440000","name":"testdomain"}` + reader := strings.NewReader(body) + req, err := http.NewRequest("POST", "/v1/sms/domain", reader) + if err != nil { + t.Fatal(err) + } + + rr := httptest.NewRecorder() + hr := http.HandlerFunc(h.createSecretDomainHandler) + + hr.ServeHTTP(rr, req) + if rr.Code != http.StatusCreated { + t.Errorf("Expected statusCreated return code. Got: %v", rr.Code) + } + + expected := smsbackend.SecretDomain{ + UUID: "123e4567-e89b-12d3-a456-426655440000", + Name: "testdomain", + } + + got := smsbackend.SecretDomain{} + json.NewDecoder(rr.Body).Decode(&got) + + if reflect.DeepEqual(expected, got) == false { + t.Errorf("CreateSecretDomainHandler returned unexpected body: got %v;"+ + " expected %v", got, expected) + } +} + +func TestCreateSecretHandler(t *testing.T) { + data := smsbackend.Secret{ + Name: "testsecret", + Values: map[string]interface{}{ + "name": "john", + "age": 43, + "isadmin": true, + }, + } + + jdata, err := json.Marshal(data) + req, err := http.NewRequest("POST", "/v1/sms/domain/testdomain/secret", bytes.NewReader(jdata)) + if err != nil { + t.Fatal(err) + } + + rr := httptest.NewRecorder() + hr := http.HandlerFunc(h.createSecretHandler) + + hr.ServeHTTP(rr, req) + if rr.Code != http.StatusCreated { + t.Errorf("Expected statusCreated return code. Got: %v", rr.Code) + } +} + +func TestDeleteSecretDomainHandler(t *testing.T) { + req, err := http.NewRequest("DELETE", "/v1/sms/domain/testdomain", nil) + if err != nil { + t.Fatal(err) + } + + rr := httptest.NewRecorder() + hr := http.HandlerFunc(h.deleteSecretDomainHandler) + + hr.ServeHTTP(rr, req) + if rr.Code != http.StatusNoContent { + t.Errorf("Expected statusCreated return code. Got: %v", rr.Code) + } +} + +func TestDeleteSecretHandler(t *testing.T) { + req, err := http.NewRequest("DELETE", "/v1/sms/domain/testdomain/secret/testsecret", nil) + if err != nil { + t.Fatal(err) + } + + rr := httptest.NewRecorder() + hr := http.HandlerFunc(h.deleteSecretHandler) + + hr.ServeHTTP(rr, req) + if rr.Code != http.StatusOK { + t.Errorf("Expected statusCreated return code. Got: %v", rr.Code) + } +} + +func TestGetSecretHandler(t *testing.T) { + req, err := http.NewRequest("DELETE", "/v1/sms/domain/testdomain/secret/testsecret", nil) + if err != nil { + t.Fatal(err) + } + + rr := httptest.NewRecorder() + hr := http.HandlerFunc(h.getSecretHandler) + + hr.ServeHTTP(rr, req) + if rr.Code != http.StatusOK { + t.Errorf("Expected statusCreated return code. Got: %v", rr.Code) + } + + expected := smsbackend.Secret{ + Name: "testsecret", + Values: map[string]interface{}{ + "profession": "engineer", + "name": "john", + }, + } + + got := smsbackend.Secret{} + json.NewDecoder(rr.Body).Decode(&got) + + if reflect.DeepEqual(expected, got) == false { + t.Errorf("CreateSecretDomainHandler returned unexpected body: got: %v"+ + " expected: %v", got, expected) + } +} + +func TestListSecretHandler(t *testing.T) { + req, err := http.NewRequest("DELETE", "/v1/sms/domain/testdomain/secret", nil) + if err != nil { + t.Fatal(err) + } + + rr := httptest.NewRecorder() + hr := http.HandlerFunc(h.listSecretHandler) + + hr.ServeHTTP(rr, req) + if rr.Code != http.StatusOK { + t.Errorf("Expected statusCreated return code. Got: %v", rr.Code) + } + + expected := []string{"testsecret1", "testsecret2"} + + var got []string + json.NewDecoder(rr.Body).Decode(&got) + + if reflect.DeepEqual(expected, got) == false { + t.Errorf("CreateSecretDomainHandler returned unexpected body: got: %v"+ + " expected: %v", got, expected) + } +} |