2018-12-09 05:19:50 +03:00
|
|
|
// Copyright 2018 The Gitea Authors. All rights reserved.
|
2022-11-27 21:20:29 +03:00
|
|
|
// SPDX-License-Identifier: MIT
|
2018-12-09 05:19:50 +03:00
|
|
|
|
2022-09-02 22:18:23 +03:00
|
|
|
package integration
|
2018-12-09 05:19:50 +03:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"net/http"
|
|
|
|
"testing"
|
|
|
|
|
2021-11-16 11:53:21 +03:00
|
|
|
"code.gitea.io/gitea/models/unittest"
|
2021-11-24 12:49:20 +03:00
|
|
|
user_model "code.gitea.io/gitea/models/user"
|
2019-05-11 13:21:34 +03:00
|
|
|
api "code.gitea.io/gitea/modules/structs"
|
2022-09-02 22:18:23 +03:00
|
|
|
"code.gitea.io/gitea/tests"
|
2019-01-24 17:12:17 +03:00
|
|
|
|
2018-12-09 05:19:50 +03:00
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestUserOrgs(t *testing.T) {
|
2022-09-02 22:18:23 +03:00
|
|
|
defer tests.PrepareTestEnv(t)()
|
2018-12-09 05:19:50 +03:00
|
|
|
adminUsername := "user1"
|
|
|
|
normalUsername := "user2"
|
2021-01-29 00:40:41 +03:00
|
|
|
privateMemberUsername := "user4"
|
|
|
|
unrelatedUsername := "user5"
|
2019-01-24 17:12:17 +03:00
|
|
|
|
2021-01-29 00:40:41 +03:00
|
|
|
orgs := getUserOrgs(t, adminUsername, normalUsername)
|
|
|
|
|
2022-08-16 05:22:25 +03:00
|
|
|
user3 := unittest.AssertExistsAndLoadBean(t, &user_model.User{Name: "user3"})
|
2022-08-21 19:24:05 +03:00
|
|
|
user17 := unittest.AssertExistsAndLoadBean(t, &user_model.User{Name: "user17"})
|
2018-12-09 05:19:50 +03:00
|
|
|
|
|
|
|
assert.Equal(t, []*api.Organization{
|
2022-08-21 19:24:05 +03:00
|
|
|
{
|
|
|
|
ID: 17,
|
2022-09-29 06:27:33 +03:00
|
|
|
Name: user17.Name,
|
2022-08-21 19:24:05 +03:00
|
|
|
UserName: user17.Name,
|
|
|
|
FullName: user17.FullName,
|
|
|
|
AvatarURL: user17.AvatarLink(),
|
|
|
|
Description: "",
|
|
|
|
Website: "",
|
|
|
|
Location: "",
|
|
|
|
Visibility: "public",
|
|
|
|
},
|
2018-12-09 05:19:50 +03:00
|
|
|
{
|
|
|
|
ID: 3,
|
2022-09-29 06:27:33 +03:00
|
|
|
Name: user3.Name,
|
2019-01-24 17:12:17 +03:00
|
|
|
UserName: user3.Name,
|
|
|
|
FullName: user3.FullName,
|
|
|
|
AvatarURL: user3.AvatarLink(),
|
2018-12-09 05:19:50 +03:00
|
|
|
Description: "",
|
|
|
|
Website: "",
|
|
|
|
Location: "",
|
2019-05-30 20:57:55 +03:00
|
|
|
Visibility: "public",
|
2018-12-09 05:19:50 +03:00
|
|
|
},
|
|
|
|
}, orgs)
|
2021-01-29 00:40:41 +03:00
|
|
|
|
|
|
|
// user itself should get it's org's he is a member of
|
|
|
|
orgs = getUserOrgs(t, privateMemberUsername, privateMemberUsername)
|
|
|
|
assert.Len(t, orgs, 1)
|
|
|
|
|
|
|
|
// unrelated user should not get private org membership of privateMemberUsername
|
|
|
|
orgs = getUserOrgs(t, unrelatedUsername, privateMemberUsername)
|
|
|
|
assert.Len(t, orgs, 0)
|
|
|
|
|
|
|
|
// not authenticated call also should hide org membership
|
|
|
|
orgs = getUserOrgs(t, "", privateMemberUsername)
|
|
|
|
assert.Len(t, orgs, 0)
|
|
|
|
}
|
|
|
|
|
|
|
|
func getUserOrgs(t *testing.T, userDoer, userCheck string) (orgs []*api.Organization) {
|
2022-01-20 20:46:10 +03:00
|
|
|
token := ""
|
2021-01-29 00:40:41 +03:00
|
|
|
if len(userDoer) != 0 {
|
2022-12-02 06:39:42 +03:00
|
|
|
token = getUserToken(t, userDoer)
|
2021-01-29 00:40:41 +03:00
|
|
|
}
|
|
|
|
urlStr := fmt.Sprintf("/api/v1/users/%s/orgs?token=%s", userCheck, token)
|
|
|
|
req := NewRequest(t, "GET", urlStr)
|
2022-12-02 06:39:42 +03:00
|
|
|
resp := MakeRequest(t, req, http.StatusOK)
|
2021-01-29 00:40:41 +03:00
|
|
|
DecodeJSON(t, resp, &orgs)
|
|
|
|
return orgs
|
2018-12-09 05:19:50 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestMyOrgs(t *testing.T) {
|
2022-09-02 22:18:23 +03:00
|
|
|
defer tests.PrepareTestEnv(t)()
|
2018-12-09 05:19:50 +03:00
|
|
|
|
2021-01-29 00:40:41 +03:00
|
|
|
req := NewRequest(t, "GET", "/api/v1/user/orgs")
|
2022-12-02 06:39:42 +03:00
|
|
|
MakeRequest(t, req, http.StatusUnauthorized)
|
2021-01-29 00:40:41 +03:00
|
|
|
|
2018-12-09 05:19:50 +03:00
|
|
|
normalUsername := "user2"
|
2022-12-02 06:39:42 +03:00
|
|
|
token := getUserToken(t, normalUsername)
|
2021-01-29 00:40:41 +03:00
|
|
|
req = NewRequest(t, "GET", "/api/v1/user/orgs?token="+token)
|
2022-12-02 06:39:42 +03:00
|
|
|
resp := MakeRequest(t, req, http.StatusOK)
|
2018-12-09 05:19:50 +03:00
|
|
|
var orgs []*api.Organization
|
|
|
|
DecodeJSON(t, resp, &orgs)
|
2022-08-16 05:22:25 +03:00
|
|
|
user3 := unittest.AssertExistsAndLoadBean(t, &user_model.User{Name: "user3"})
|
2022-08-21 19:24:05 +03:00
|
|
|
user17 := unittest.AssertExistsAndLoadBean(t, &user_model.User{Name: "user17"})
|
2018-12-09 05:19:50 +03:00
|
|
|
|
|
|
|
assert.Equal(t, []*api.Organization{
|
2022-08-21 19:24:05 +03:00
|
|
|
{
|
|
|
|
ID: 17,
|
2022-09-29 06:27:33 +03:00
|
|
|
Name: user17.Name,
|
2022-08-21 19:24:05 +03:00
|
|
|
UserName: user17.Name,
|
|
|
|
FullName: user17.FullName,
|
|
|
|
AvatarURL: user17.AvatarLink(),
|
|
|
|
Description: "",
|
|
|
|
Website: "",
|
|
|
|
Location: "",
|
|
|
|
Visibility: "public",
|
|
|
|
},
|
2018-12-09 05:19:50 +03:00
|
|
|
{
|
|
|
|
ID: 3,
|
2022-09-29 06:27:33 +03:00
|
|
|
Name: user3.Name,
|
2019-01-24 17:12:17 +03:00
|
|
|
UserName: user3.Name,
|
|
|
|
FullName: user3.FullName,
|
|
|
|
AvatarURL: user3.AvatarLink(),
|
2018-12-09 05:19:50 +03:00
|
|
|
Description: "",
|
|
|
|
Website: "",
|
|
|
|
Location: "",
|
2019-05-30 20:57:55 +03:00
|
|
|
Visibility: "public",
|
2018-12-09 05:19:50 +03:00
|
|
|
},
|
|
|
|
}, orgs)
|
|
|
|
}
|