Skip to content

Commit

Permalink
chore: Add tests for sprint close (#778)
Browse files Browse the repository at this point in the history
  • Loading branch information
ankitpokhrel authored Sep 20, 2024
1 parent 05564e8 commit 3422178
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 1 deletion.
5 changes: 4 additions & 1 deletion pkg/jira/sprint.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,8 +88,11 @@ func (c *Client) EndSprint(sprintID int) error {
if err != nil {
return err
}
if sprint.Status == SprintStateClosed {
return fmt.Errorf("sprint %d is already closed", sprintID)
}

// update to closed and format for PUT
// Update to closed and format for PUT.
sprint.Status = SprintStateClosed
body, err := json.Marshal(sprint)
if err != nil {
Expand Down
74 changes: 74 additions & 0 deletions pkg/jira/sprint_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -342,3 +342,77 @@ func TestSprintIssuesAdd(t *testing.T) {
err = client.SprintIssuesAdd("5", "TEST-1")
assert.Error(t, &ErrUnexpectedResponse{}, err)
}

func TestGetSprint(t *testing.T) {
var unexpectedStatusCode bool

server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
assert.Equal(t, "/rest/agile/1.0/sprint/5", r.URL.Path)

if unexpectedStatusCode {
w.WriteHeader(400)
} else {
assert.Equal(t, "GET", r.Method)

resp, err := os.ReadFile("./testdata/sprint-get.json")
assert.NoError(t, err)

w.Header().Set("Content-Type", "application/json")
w.WriteHeader(200)
_, _ = w.Write(resp)
}
}))
defer server.Close()

client := NewClient(Config{Server: server.URL}, WithTimeout(3*time.Second))

sprint, err := client.GetSprint(5)
assert.NoError(t, err)
assert.Equal(t, sprint.ID, 5)
assert.Equal(t, sprint.Status, "active")

unexpectedStatusCode = true

_, err = client.GetSprint(5)
assert.Error(t, &ErrUnexpectedResponse{}, err)
}

func TestEndSprint(t *testing.T) {
var unexpectedStatusCode bool

server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
assert.NotNilf(t, r.Method, "invalid request method")

if r.Method == "GET" {
assert.Equal(t, "/rest/agile/1.0/sprint/5", r.URL.Path)

resp, err := os.ReadFile("./testdata/sprint-get.json")
assert.NoError(t, err)

w.Header().Set("Content-Type", "application/json")
w.WriteHeader(200)
_, _ = w.Write(resp)
} else {
if unexpectedStatusCode {
w.WriteHeader(400)
} else if r.Method == "PUT" {
assert.Equal(t, "PUT", r.Method)
assert.Equal(t, "application/json", r.Header.Get("Accept"))
assert.Equal(t, "application/json", r.Header.Get("Content-Type"))

w.WriteHeader(200)
}
}
}))
defer server.Close()

client := NewClient(Config{Server: server.URL}, WithTimeout(3*time.Second))

err := client.EndSprint(5)
assert.NoError(t, err)

unexpectedStatusCode = true

err = client.EndSprint(5)
assert.Error(t, &ErrUnexpectedResponse{}, err)
}
11 changes: 11 additions & 0 deletions pkg/jira/testdata/sprint-get.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"id": 5,
"self": "https://demo.atlassian.net/rest/agile/1.0/sprint/5",
"state": "active",
"name": "sprint 1",
"startDate": "2025-04-11T15:22:00.000+10:00",
"endDate": "2025-04-20T01:22:00.000+10:00",
"completeDate": "2025-04-20T11:04:00.000+10:00",
"originBoardId": 3,
"goal": "sprint 1 goal"
}

0 comments on commit 3422178

Please sign in to comment.