|
5 | 5 | package integrations
|
6 | 6 |
|
7 | 7 | import (
|
| 8 | + "fmt" |
8 | 9 | "net/http"
|
9 | 10 | "strings"
|
10 | 11 | "testing"
|
11 | 12 |
|
| 13 | + api "code.gitea.io/gitea/modules/structs" |
12 | 14 | "github.com/stretchr/testify/assert"
|
13 | 15 | )
|
14 | 16 |
|
@@ -110,3 +112,64 @@ func TestPrivateOrg(t *testing.T) {
|
110 | 112 | req = NewRequest(t, "GET", "/privated_org/private_repo_on_private_org")
|
111 | 113 | session.MakeRequest(t, req, http.StatusOK)
|
112 | 114 | }
|
| 115 | + |
| 116 | +func TestOrgRestrictedUser(t *testing.T) { |
| 117 | + defer prepareTestEnv(t)() |
| 118 | + |
| 119 | + // privated_org is a private org who has id 23 |
| 120 | + orgName := "privated_org" |
| 121 | + |
| 122 | + // public_repo_on_private_org is a public repo on privated_org |
| 123 | + repoName := "public_repo_on_private_org" |
| 124 | + |
| 125 | + // user29 is a restricted user who is not a member of the organization |
| 126 | + restrictedUser := "user29" |
| 127 | + |
| 128 | + // #17003 reports a bug whereby adding a restricted user to a read-only team doesn't work |
| 129 | + |
| 130 | + // assert restrictedUser cannot see the org or the public repo |
| 131 | + restrictedSession := loginUser(t, restrictedUser) |
| 132 | + req := NewRequest(t, "GET", fmt.Sprintf("/%s", orgName)) |
| 133 | + restrictedSession.MakeRequest(t, req, http.StatusNotFound) |
| 134 | + |
| 135 | + req = NewRequest(t, "GET", fmt.Sprintf("/%s/%s", orgName, repoName)) |
| 136 | + restrictedSession.MakeRequest(t, req, http.StatusNotFound) |
| 137 | + |
| 138 | + // Therefore create a read-only team |
| 139 | + adminSession := loginUser(t, "user1") |
| 140 | + token := getTokenForLoggedInUser(t, adminSession) |
| 141 | + |
| 142 | + teamToCreate := &api.CreateTeamOption{ |
| 143 | + Name: "codereader", |
| 144 | + Description: "Code Reader", |
| 145 | + IncludesAllRepositories: true, |
| 146 | + Permission: "read", |
| 147 | + Units: []string{"repo.code"}, |
| 148 | + } |
| 149 | + |
| 150 | + req = NewRequestWithJSON(t, "POST", |
| 151 | + fmt.Sprintf("/api/v1/orgs/%s/teams?token=%s", orgName, token), teamToCreate) |
| 152 | + |
| 153 | + var apiTeam api.Team |
| 154 | + |
| 155 | + resp := adminSession.MakeRequest(t, req, http.StatusCreated) |
| 156 | + DecodeJSON(t, resp, &apiTeam) |
| 157 | + checkTeamResponse(t, &apiTeam, teamToCreate.Name, teamToCreate.Description, teamToCreate.IncludesAllRepositories, |
| 158 | + teamToCreate.Permission, teamToCreate.Units) |
| 159 | + checkTeamBean(t, apiTeam.ID, teamToCreate.Name, teamToCreate.Description, teamToCreate.IncludesAllRepositories, |
| 160 | + teamToCreate.Permission, teamToCreate.Units) |
| 161 | + //teamID := apiTeam.ID |
| 162 | + |
| 163 | + // Now we need to add the restricted user to the team |
| 164 | + req = NewRequest(t, "PUT", |
| 165 | + fmt.Sprintf("/api/v1/teams/%d/members/%s?token=%s", apiTeam.ID, restrictedUser, token)) |
| 166 | + _ = adminSession.MakeRequest(t, req, http.StatusNoContent) |
| 167 | + |
| 168 | + // Now we need to check if the restrictedUser can access the repo |
| 169 | + req = NewRequest(t, "GET", fmt.Sprintf("/%s", orgName)) |
| 170 | + restrictedSession.MakeRequest(t, req, http.StatusOK) |
| 171 | + |
| 172 | + req = NewRequest(t, "GET", fmt.Sprintf("/%s/%s", orgName, repoName)) |
| 173 | + restrictedSession.MakeRequest(t, req, http.StatusOK) |
| 174 | + |
| 175 | +} |
0 commit comments