From e75e7750f2d2944a824cd8f3a2b65b35fc227c66 Mon Sep 17 00:00:00 2001 From: Bidesh Thapaliya Date: Tue, 27 Sep 2016 13:26:07 +0200 Subject: [PATCH] Adds test for authentication on expected json. Adds test to metaissue --- authentication.go | 3 +++ authentication_test.go | 43 ++++++++++++++++++++++++++++++++--- metaissue_test.go | 51 ++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 94 insertions(+), 3 deletions(-) diff --git a/authentication.go b/authentication.go index e326779..59fdf88 100644 --- a/authentication.go +++ b/authentication.go @@ -133,6 +133,9 @@ func (s *AuthenticationService) GetCurrentUser() (*Session, error) { if err != nil { return nil, fmt.Errorf("Error sending request to get user info : %s", err) } + if resp.StatusCode != 200 { + return nil, fmt.Errorf("Getting user info failed with status : %d", resp.StatusCode) + } defer resp.Body.Close() ret := new(Session) diff --git a/authentication_test.go b/authentication_test.go index 2d1c593..89e60ca 100644 --- a/authentication_test.go +++ b/authentication_test.go @@ -86,6 +86,44 @@ func TestAuthenticationService_Authenticated(t *testing.T) { } } +func TestAithenticationService_GetUserInfo_AccessForbidden_Fail(t *testing.T) { + setup() + defer teardown() + testMux.HandleFunc("/rest/auth/1/session", func(w http.ResponseWriter, r *http.Request) { + if r.Method == "POST" { + testMethod(t, r, "POST") + testRequestURL(t, r, "/rest/auth/1/session") + b, err := ioutil.ReadAll(r.Body) + if err != nil { + t.Errorf("Error in read body: %s", err) + } + if bytes.Index(b, []byte(`"username":"foo"`)) < 0 { + t.Error("No username found") + } + if bytes.Index(b, []byte(`"password":"bar"`)) < 0 { + t.Error("No password found") + } + + fmt.Fprint(w, `{"session":{"name":"JSESSIONID","value":"12345678901234567890"},"loginInfo":{"failedLoginCount":10,"loginCount":127,"lastFailedLoginTime":"2016-03-16T04:22:35.386+0000","previousLoginTime":"2016-03-16T04:22:35.386+0000"}}`) + } + + if r.Method == "GET" { + testMethod(t, r, "GET") + testRequestURL(t, r, "/rest/auth/1/session") + + w.WriteHeader(http.StatusForbidden) + } + }) + + testClient.Authentication.AcquireSessionCookie("foo", "bar") + + _, err := testClient.Authentication.GetCurrentUser() + if err == nil { + t.Errorf("Non nil error expect, recieved nil") + } + +} + func TestAuthenticationService_GetUserInfo_NonOkStatusCode_Fail(t *testing.T) { setup() defer teardown() @@ -111,7 +149,8 @@ func TestAuthenticationService_GetUserInfo_NonOkStatusCode_Fail(t *testing.T) { if r.Method == "GET" { testMethod(t, r, "GET") testRequestURL(t, r, "/rest/auth/1/session") - w.WriteHeader(http.StatusForbidden) + //any status but 200 + w.WriteHeader(240) } }) @@ -233,10 +272,8 @@ func TestAuthenticationService_Logout_FailWithoutLogin(t *testing.T) { } }) - err := testClient.Authentication.Logout() if err == nil { t.Error("Expected not nil, got nil") } - } diff --git a/metaissue_test.go b/metaissue_test.go index 0fdf995..0f53e23 100644 --- a/metaissue_test.go +++ b/metaissue_test.go @@ -410,6 +410,40 @@ func TestMetaIssueTypes_GetMandatoryFields(t *testing.T) { } +func TestMetaIssueTypes_GetMandatoryFields_NonExistentRequiredKey_Fail(t *testing.T) { + data := make(map[string]interface{}) + + data["summary"] = map[string]interface{}{ + "name": "Summary", + } + + m := new(MetaIssueTypes) + m.Fields = data + + _, err := m.GetMandatoryFields() + if err == nil { + t.Error("Expected non nil errpr, recieved nil") + } + +} + +func TestMetaIssueTypes_GetMandatoryFields_NonExistentNameKey_Fail(t *testing.T) { + data := make(map[string]interface{}) + + data["summary"] = map[string]interface{}{ + "required": true, + } + + m := new(MetaIssueTypes) + m.Fields = data + + _, err := m.GetMandatoryFields() + if err == nil { + t.Error("Expected non nil errpr, recieved nil") + } + +} + func TestMetaIssueTypes_GetAllFields(t *testing.T) { data := make(map[string]interface{}) @@ -443,6 +477,23 @@ func TestMetaIssueTypes_GetAllFields(t *testing.T) { } +func TestMetaIssueTypes_GetAllFields_NonExistingNameKey_Fail(t *testing.T) { + data := make(map[string]interface{}) + + data["summary"] = map[string]interface{}{ + "required": true, + } + + m := new(MetaIssueTypes) + m.Fields = data + + _, err := m.GetAllFields() + if err == nil { + t.Error("Expected non nil error, recieved nil") + } + +} + func TestCreateMetaInfo_GetProjectName_Success(t *testing.T) { metainfo := new(CreateMetaInfo) metainfo.Projects = append(metainfo.Projects, &MetaProject{