|
| 1 | +// Copyright 2017 The Gitea Authors. All rights reserved. |
| 2 | +// SPDX-License-Identifier: MIT |
| 3 | + |
| 4 | +package integration |
| 5 | + |
| 6 | +import ( |
| 7 | + "fmt" |
| 8 | + "net/http" |
| 9 | + "testing" |
| 10 | + |
| 11 | + auth_model "code.gitea.io/gitea/models/auth" |
| 12 | + "code.gitea.io/gitea/models/unittest" |
| 13 | + user_model "code.gitea.io/gitea/models/user" |
| 14 | + api "code.gitea.io/gitea/modules/structs" |
| 15 | + "code.gitea.io/gitea/tests" |
| 16 | + "github.com/stretchr/testify/assert" |
| 17 | +) |
| 18 | + |
| 19 | +func TestAPICreateOrgProject(t *testing.T) { |
| 20 | + createOrgProjectSuccessTestCases := []struct { |
| 21 | + testName string |
| 22 | + orgName string |
| 23 | + ctxUserID int64 |
| 24 | + doerID int64 |
| 25 | + title string |
| 26 | + content string |
| 27 | + templateType uint8 |
| 28 | + cardType uint8 |
| 29 | + }{ |
| 30 | + { |
| 31 | + testName: "site admin create project successfully", |
| 32 | + ctxUserID: 3, |
| 33 | + doerID: 1, |
| 34 | + title: "site-admin", |
| 35 | + content: "project_description", |
| 36 | + templateType: 1, |
| 37 | + cardType: 2, |
| 38 | + }, |
| 39 | + { |
| 40 | + testName: "org owner create project successfully", |
| 41 | + ctxUserID: 3, |
| 42 | + doerID: 2, |
| 43 | + title: "org-owner", |
| 44 | + content: "project_description", |
| 45 | + templateType: 1, |
| 46 | + cardType: 2, |
| 47 | + }, |
| 48 | + { |
| 49 | + testName: "member create project successfully with write access", |
| 50 | + ctxUserID: 3, |
| 51 | + doerID: 4, |
| 52 | + title: "member-with-write-access", |
| 53 | + content: "project_description", |
| 54 | + templateType: 1, |
| 55 | + cardType: 2, |
| 56 | + }, |
| 57 | + } |
| 58 | + |
| 59 | + createOrgProjectFailTestCases := []struct { |
| 60 | + testName string |
| 61 | + orgName string |
| 62 | + ctxUserID int64 |
| 63 | + doerID int64 |
| 64 | + title string |
| 65 | + expectedStatus int |
| 66 | + }{ |
| 67 | + { |
| 68 | + testName: "user is not in organization", |
| 69 | + orgName: "org3", |
| 70 | + ctxUserID: 3, |
| 71 | + doerID: 5, |
| 72 | + title: "user-not-in-org", |
| 73 | + expectedStatus: http.StatusForbidden, |
| 74 | + }, |
| 75 | + { |
| 76 | + testName: "user is member but not sufficient access", |
| 77 | + orgName: "org17", |
| 78 | + ctxUserID: 17, |
| 79 | + doerID: 20, |
| 80 | + title: "member-not-sufficient-access", |
| 81 | + expectedStatus: http.StatusForbidden, |
| 82 | + }, |
| 83 | + { |
| 84 | + testName: "project not created as title is empty", |
| 85 | + orgName: "org3", |
| 86 | + ctxUserID: 3, |
| 87 | + doerID: 2, |
| 88 | + title: "", |
| 89 | + expectedStatus: http.StatusUnprocessableEntity, |
| 90 | + }, |
| 91 | + { |
| 92 | + testName: "project not created as title is too long", |
| 93 | + orgName: "org3", |
| 94 | + ctxUserID: 3, |
| 95 | + doerID: 2, |
| 96 | + title: "This is a very long title that will exceed the maximum allowed size of 100 characters. It keeps going beyond the limit.", |
| 97 | + expectedStatus: http.StatusUnprocessableEntity, |
| 98 | + }, |
| 99 | + } |
| 100 | + |
| 101 | + defer tests.PrepareTestEnv(t)() |
| 102 | + |
| 103 | + for _, tt := range createOrgProjectFailTestCases { |
| 104 | + t.Run(tt.testName, func(t *testing.T) { |
| 105 | + user := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: tt.doerID}) |
| 106 | + session := loginUser(t, user.Name) |
| 107 | + token := getTokenForLoggedInUser(t, session, auth_model.AccessTokenScopeWriteAdmin, auth_model.AccessTokenScopeWriteOrganization) |
| 108 | + req := NewRequestWithJSON(t, "POST", fmt.Sprintf("/api/v1/orgs/%s/projects", tt.orgName), &api.CreateProjectOption{ |
| 109 | + Title: tt.title, |
| 110 | + }).AddTokenAuth(token) |
| 111 | + MakeRequest(t, req, tt.expectedStatus) |
| 112 | + }) |
| 113 | + } |
| 114 | + |
| 115 | + for _, tt := range createOrgProjectSuccessTestCases { |
| 116 | + t.Run(tt.testName, func(t *testing.T) { |
| 117 | + user := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: tt.doerID}) |
| 118 | + session := loginUser(t, user.Name) |
| 119 | + token := getTokenForLoggedInUser(t, session, auth_model.AccessTokenScopeWriteAdmin, auth_model.AccessTokenScopeWriteOrganization) |
| 120 | + req := NewRequestWithJSON(t, "POST", "/api/v1/orgs/org3/projects", &api.CreateProjectOption{ |
| 121 | + Title: tt.title, |
| 122 | + Content: tt.content, |
| 123 | + TemplateType: tt.templateType, |
| 124 | + CardType: tt.cardType, |
| 125 | + }).AddTokenAuth(token) |
| 126 | + resp := MakeRequest(t, req, http.StatusCreated) |
| 127 | + var apiProject api.Project |
| 128 | + DecodeJSON(t, resp, &apiProject) |
| 129 | + assert.Equal(t, tt.title, apiProject.Title) |
| 130 | + assert.Equal(t, tt.content, apiProject.Description) |
| 131 | + assert.Equal(t, tt.templateType, apiProject.TemplateType) |
| 132 | + assert.Equal(t, tt.cardType, apiProject.CardType) |
| 133 | + assert.Equal(t, tt.ctxUserID, apiProject.OwnerID) |
| 134 | + assert.Equal(t, tt.doerID, apiProject.CreatorID) |
| 135 | + }) |
| 136 | + } |
| 137 | +} |
| 138 | + |
| 139 | +func TestAPIGetOrgProjects(t *testing.T) { |
| 140 | + |
| 141 | + defer tests.PrepareTestEnv(t)() |
| 142 | + |
| 143 | + user := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 2}) |
| 144 | + session := loginUser(t, user.Name) |
| 145 | + token := getTokenForLoggedInUser(t, session, auth_model.AccessTokenScopeReadAdmin, auth_model.AccessTokenScopeReadOrganization) |
| 146 | + |
| 147 | + expectedProjects := []*api.Project{ |
| 148 | + { |
| 149 | + Title: "project1 belongs to org3", |
| 150 | + OwnerID: 3, |
| 151 | + IsClosed: true, |
| 152 | + CreatorID: 3, |
| 153 | + TemplateType: 1, |
| 154 | + CardType: 2, |
| 155 | + }, |
| 156 | + { |
| 157 | + Title: "project2 belongs to org3", |
| 158 | + OwnerID: 3, |
| 159 | + IsClosed: false, |
| 160 | + CreatorID: 3, |
| 161 | + TemplateType: 1, |
| 162 | + CardType: 2, |
| 163 | + }, |
| 164 | + } |
| 165 | + |
| 166 | + t.Run("failed to get projects org not found", func(t *testing.T) { |
| 167 | + req := NewRequest(t, "GET", "/api/v1/orgs/org90/projects").AddTokenAuth(token) |
| 168 | + MakeRequest(t, req, http.StatusNotFound) |
| 169 | + }) |
| 170 | + t.Run("get projects successfully", func(t *testing.T) { |
| 171 | + req := NewRequest(t, "GET", "/api/v1/orgs/org3/projects").AddTokenAuth(token) |
| 172 | + resp := MakeRequest(t, req, http.StatusOK) |
| 173 | + var apiProjects []*api.Project |
| 174 | + DecodeJSON(t, resp, &apiProjects) |
| 175 | + assert.Equal(t, len(expectedProjects), len(apiProjects)) |
| 176 | + for i, expectedProject := range expectedProjects { |
| 177 | + assert.Equal(t, expectedProject.Title, apiProjects[i].Title) |
| 178 | + assert.Equal(t, expectedProject.OwnerID, apiProjects[i].OwnerID) |
| 179 | + assert.Equal(t, expectedProject.IsClosed, apiProjects[i].IsClosed) |
| 180 | + assert.Equal(t, expectedProject.CreatorID, apiProjects[i].CreatorID) |
| 181 | + assert.Equal(t, expectedProject.TemplateType, apiProjects[i].TemplateType) |
| 182 | + assert.Equal(t, expectedProject.CardType, apiProjects[i].CardType) |
| 183 | + } |
| 184 | + }) |
| 185 | +} |
0 commit comments