Skip to content

Commit

Permalink
Increased tests in attachment, authentication and work-package
Browse files Browse the repository at this point in the history
  • Loading branch information
manuelbcd committed Mar 30, 2021
1 parent 513b664 commit 1f44d93
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 89 deletions.
18 changes: 18 additions & 0 deletions attachment_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,3 +62,21 @@ func TestAttachmentService_Download(t *testing.T) {
t.Errorf("Unexpected downloaded filesize %d", len(*file))
}
}

func TestAttachmentService_Download_BadStatus(t *testing.T) {
setup()
defer teardown()
testAPIEdpoint := "/api/v3/attachments/5/content"

testMux.HandleFunc(testAPIEdpoint, func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, "GET")
testRequestURL(t, r, testAPIEdpoint)
w.WriteHeader(http.StatusForbidden)
})

_, err := testClient.Attachment.Download("5")

if err == nil {
t.Errorf("Error %d expected but null received", http.StatusForbidden)
}
}
36 changes: 0 additions & 36 deletions authentication.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,42 +113,6 @@ func (s *AuthenticationService) Authenticated() bool {
return false
}

// LogoutWithContext logs out the current user that has been authenticated and the session in the client is destroyed.
// Deprecated: Use CookieAuthTransport to create base client. Logging out is as simple as not using the
// client anymore
func (s *AuthenticationService) LogoutWithContext(ctx context.Context) error {
if s.authType != authTypeSession || s.client.session == nil {
return fmt.Errorf("no user is authenticated")
}

apiEndpoint := "rest/auth/1/session"
req, err := s.client.NewRequestWithContext(ctx, "DELETE", apiEndpoint, nil)
if err != nil {
return fmt.Errorf("creating the request to log the user out failed : %s", err)
}

resp, err := s.client.Do(req, nil)
if err != nil {
return fmt.Errorf("error sending the logout request: %s", err)
}
if resp.StatusCode != 204 {
return fmt.Errorf("the logout was unsuccessful with status %d", resp.StatusCode)
}

// If logout successful, delete session
s.client.session = nil

return nil

}

// Logout wraps LogoutWithContext using the background context.
// Deprecated: Use CookieAuthTransport to create base client. Logging out is as simple as not using the
// client anymore
func (s *AuthenticationService) Logout() error {
return s.LogoutWithContext(context.Background())
}

// GetCurrentUserWithContext gets the details of the current user.
func (s *AuthenticationService) GetCurrentUserWithContext(ctx context.Context) (*Session, error) {
if s == nil {
Expand Down
52 changes: 0 additions & 52 deletions authentication_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -267,55 +267,3 @@ func TestAuthenticationService_GetUserInfo_Success(t *testing.T) {
t.Error("The user information doesn't match")
}
}

func TestAuthenticationService_Logout_Success(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.Contains(b, []byte(`"username":"foo"`)) {
t.Error("No username found")
}
if !bytes.Contains(b, []byte(`"password":"bar"`)) {
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 == "DELETE" {
// return 204
w.WriteHeader(http.StatusNoContent)
}
})

testClient.Authentication.AcquireSessionCookie("foo", "bar")

err := testClient.Authentication.Logout()
if err != nil {
t.Errorf("Expected nil error, got %s", err)
}
}

func TestAuthenticationService_Logout_FailWithoutLogin(t *testing.T) {
setup()
defer teardown()

testMux.HandleFunc("/rest/auth/1/session", func(w http.ResponseWriter, r *http.Request) {
if r.Method == "DELETE" {
// 401
w.WriteHeader(http.StatusUnauthorized)
}
})
err := testClient.Authentication.Logout()
if err == nil {
t.Error("Expected not nil, got nil")
}
}
11 changes: 10 additions & 1 deletion work-package_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"net/http"
"reflect"
"testing"
"time"
)

func TestWorkPackageService_Get_Success(t *testing.T) {
Expand Down Expand Up @@ -122,13 +123,17 @@ func TestWorkPackageService_Delete(t *testing.T) {
}

func TestWorkPackageFields_MarshalJSON_OmitsEmptyFields(t *testing.T) {
thisDate, _ := time.Parse("2006-01-02T15:04:05-0700", "2020-12-31")
thisDateTime := Time(thisDate)

i := &WorkPackage{
Description: &WPDescription{
Format: "html",
Raw: "Content example",
HTML: "<html>Content example</html>",
},
Type: "Task",
CreatedAt: &thisDateTime,
Type: "Task",
}

rawdata, err := json.Marshal(i)
Expand Down Expand Up @@ -161,12 +166,16 @@ func TestWorkPackageFields_MarshalJSON_OmitsEmptyFields(t *testing.T) {
}

func TestWorkPackageCustomFields_MarshalJSON_Success(t *testing.T) {
thisDate, _ := time.Parse("2006-01-02T15:04:05-0700", "2020-12-31")
thisDateTime := Time(thisDate)

i := &WorkPackage{
Description: &WPDescription{
Format: "html",
Raw: "Content example",
HTML: "<html>Content example</html>",
},
CreatedAt: &thisDateTime,
Custom: tcontainer.MarshalMap{
"customfield_A": "testA",
},
Expand Down

0 comments on commit 1f44d93

Please sign in to comment.