1
0
mirror of https://github.com/interviewstreet/go-jira.git synced 2025-01-05 22:53:53 +02:00

Adds test for authentication on expected json. Adds test to metaissue

This commit is contained in:
Bidesh Thapaliya 2016-09-27 13:26:07 +02:00
parent d3ec8f16c0
commit e75e7750f2
3 changed files with 94 additions and 3 deletions

View File

@ -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)

View File

@ -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")
}
}

View File

@ -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{