2020-01-29 14:36:32 +08:00
|
|
|
// Copyright 2020 The Gitea Authors. All rights reserved.
|
2022-11-28 02:20:29 +08:00
|
|
|
// SPDX-License-Identifier: MIT
|
2020-01-29 14:36:32 +08:00
|
|
|
|
2022-09-03 03:18:23 +08:00
|
|
|
package integration
|
2020-01-29 14:36:32 +08:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"net/http"
|
|
|
|
"testing"
|
|
|
|
|
2023-01-18 05:46:03 +08:00
|
|
|
auth_model "code.gitea.io/gitea/models/auth"
|
2022-04-08 17:11:15 +08:00
|
|
|
issues_model "code.gitea.io/gitea/models/issues"
|
2021-12-10 09:27:50 +08:00
|
|
|
repo_model "code.gitea.io/gitea/models/repo"
|
2021-11-16 16:53:21 +08:00
|
|
|
"code.gitea.io/gitea/models/unittest"
|
2021-11-24 17:49:20 +08:00
|
|
|
user_model "code.gitea.io/gitea/models/user"
|
2020-01-29 14:36:32 +08:00
|
|
|
"code.gitea.io/gitea/modules/structs"
|
2022-09-03 03:18:23 +08:00
|
|
|
"code.gitea.io/gitea/tests"
|
2020-01-29 14:36:32 +08:00
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestAPIIssuesMilestone(t *testing.T) {
|
2022-09-03 03:18:23 +08:00
|
|
|
defer tests.PrepareTestEnv(t)()
|
2020-01-29 14:36:32 +08:00
|
|
|
|
2022-08-16 10:22:25 +08:00
|
|
|
milestone := unittest.AssertExistsAndLoadBean(t, &issues_model.Milestone{ID: 1})
|
|
|
|
repo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: milestone.RepoID})
|
|
|
|
owner := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: repo.OwnerID})
|
2020-01-29 14:36:32 +08:00
|
|
|
assert.Equal(t, int64(1), int64(milestone.NumIssues))
|
|
|
|
assert.Equal(t, structs.StateOpen, milestone.State())
|
|
|
|
|
|
|
|
session := loginUser(t, owner.Name)
|
2023-01-18 05:46:03 +08:00
|
|
|
token := getTokenForLoggedInUser(t, session, auth_model.AccessTokenScopeRepo)
|
2020-01-29 14:36:32 +08:00
|
|
|
|
|
|
|
// update values of issue
|
|
|
|
milestoneState := "closed"
|
|
|
|
|
|
|
|
urlStr := fmt.Sprintf("/api/v1/repos/%s/%s/milestones/%d?token=%s", owner.Name, repo.Name, milestone.ID, token)
|
|
|
|
req := NewRequestWithJSON(t, "PATCH", urlStr, structs.EditMilestoneOption{
|
|
|
|
State: &milestoneState,
|
|
|
|
})
|
2022-12-02 11:39:42 +08:00
|
|
|
resp := MakeRequest(t, req, http.StatusOK)
|
2020-01-29 14:36:32 +08:00
|
|
|
var apiMilestone structs.Milestone
|
|
|
|
DecodeJSON(t, resp, &apiMilestone)
|
|
|
|
assert.EqualValues(t, "closed", apiMilestone.State)
|
|
|
|
|
|
|
|
req = NewRequest(t, "GET", urlStr)
|
2022-12-02 11:39:42 +08:00
|
|
|
resp = MakeRequest(t, req, http.StatusOK)
|
2020-01-29 14:36:32 +08:00
|
|
|
var apiMilestone2 structs.Milestone
|
|
|
|
DecodeJSON(t, resp, &apiMilestone2)
|
|
|
|
assert.EqualValues(t, "closed", apiMilestone2.State)
|
2020-06-10 06:01:36 +08:00
|
|
|
|
|
|
|
req = NewRequestWithJSON(t, "POST", fmt.Sprintf("/api/v1/repos/%s/%s/milestones?token=%s", owner.Name, repo.Name, token), structs.CreateMilestoneOption{
|
|
|
|
Title: "wow",
|
|
|
|
Description: "closed one",
|
|
|
|
State: "closed",
|
|
|
|
})
|
2022-12-02 11:39:42 +08:00
|
|
|
resp = MakeRequest(t, req, http.StatusCreated)
|
2020-06-10 06:01:36 +08:00
|
|
|
DecodeJSON(t, resp, &apiMilestone)
|
|
|
|
assert.Equal(t, "wow", apiMilestone.Title)
|
|
|
|
assert.Equal(t, structs.StateClosed, apiMilestone.State)
|
|
|
|
|
2020-07-28 19:30:40 +08:00
|
|
|
var apiMilestones []structs.Milestone
|
|
|
|
req = NewRequest(t, "GET", fmt.Sprintf("/api/v1/repos/%s/%s/milestones?state=%s&token=%s", owner.Name, repo.Name, "all", token))
|
2022-12-02 11:39:42 +08:00
|
|
|
resp = MakeRequest(t, req, http.StatusOK)
|
2020-07-28 19:30:40 +08:00
|
|
|
DecodeJSON(t, resp, &apiMilestones)
|
|
|
|
assert.Len(t, apiMilestones, 4)
|
|
|
|
|
2020-09-14 19:48:03 +08:00
|
|
|
req = NewRequest(t, "GET", fmt.Sprintf("/api/v1/repos/%s/%s/milestones/%s?token=%s", owner.Name, repo.Name, apiMilestones[2].Title, token))
|
2022-12-02 11:39:42 +08:00
|
|
|
resp = MakeRequest(t, req, http.StatusOK)
|
2020-09-14 19:48:03 +08:00
|
|
|
DecodeJSON(t, resp, &apiMilestone)
|
|
|
|
assert.EqualValues(t, apiMilestones[2], apiMilestone)
|
|
|
|
|
2020-07-28 19:30:40 +08:00
|
|
|
req = NewRequest(t, "GET", fmt.Sprintf("/api/v1/repos/%s/%s/milestones?state=%s&name=%s&token=%s", owner.Name, repo.Name, "all", "milestone2", token))
|
2022-12-02 11:39:42 +08:00
|
|
|
resp = MakeRequest(t, req, http.StatusOK)
|
2020-07-28 19:30:40 +08:00
|
|
|
DecodeJSON(t, resp, &apiMilestones)
|
|
|
|
assert.Len(t, apiMilestones, 1)
|
|
|
|
assert.Equal(t, int64(2), apiMilestones[0].ID)
|
|
|
|
|
2020-06-10 06:01:36 +08:00
|
|
|
req = NewRequest(t, "DELETE", fmt.Sprintf("/api/v1/repos/%s/%s/milestones/%d?token=%s", owner.Name, repo.Name, apiMilestone.ID, token))
|
2022-12-02 11:39:42 +08:00
|
|
|
MakeRequest(t, req, http.StatusNoContent)
|
2020-01-29 14:36:32 +08:00
|
|
|
}
|