2018-12-09 02:19:50 +00:00
|
|
|
// Copyright 2018 The Gitea Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a MIT-style
|
|
|
|
// license that can be found in the LICENSE file.package models
|
|
|
|
|
2022-09-02 19:18:23 +00:00
|
|
|
package integration
|
2018-12-09 02:19:50 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"net/http"
|
|
|
|
"testing"
|
|
|
|
|
2021-11-16 08:53:21 +00:00
|
|
|
"code.gitea.io/gitea/models/unittest"
|
2021-11-24 09:49:20 +00:00
|
|
|
user_model "code.gitea.io/gitea/models/user"
|
2019-05-11 10:21:34 +00:00
|
|
|
api "code.gitea.io/gitea/modules/structs"
|
2022-09-02 19:18:23 +00:00
|
|
|
"code.gitea.io/gitea/tests"
|
2019-01-24 14:12:17 +00:00
|
|
|
|
2018-12-09 02:19:50 +00:00
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestUserOrgs(t *testing.T) {
|
2022-09-02 19:18:23 +00:00
|
|
|
defer tests.PrepareTestEnv(t)()
|
2018-12-09 02:19:50 +00:00
|
|
|
adminUsername := "user1"
|
|
|
|
normalUsername := "user2"
|
2021-01-28 21:40:41 +00:00
|
|
|
privateMemberUsername := "user4"
|
|
|
|
unrelatedUsername := "user5"
|
2019-01-24 14:12:17 +00:00
|
|
|
|
2021-01-28 21:40:41 +00:00
|
|
|
orgs := getUserOrgs(t, adminUsername, normalUsername)
|
|
|
|
|
2022-08-16 02:22:25 +00:00
|
|
|
user3 := unittest.AssertExistsAndLoadBean(t, &user_model.User{Name: "user3"})
|
2022-08-21 16:24:05 +00:00
|
|
|
user17 := unittest.AssertExistsAndLoadBean(t, &user_model.User{Name: "user17"})
|
2018-12-09 02:19:50 +00:00
|
|
|
|
|
|
|
assert.Equal(t, []*api.Organization{
|
2022-08-21 16:24:05 +00:00
|
|
|
{
|
|
|
|
ID: 17,
|
|
|
|
UserName: user17.Name,
|
|
|
|
FullName: user17.FullName,
|
|
|
|
AvatarURL: user17.AvatarLink(),
|
|
|
|
Description: "",
|
|
|
|
Website: "",
|
|
|
|
Location: "",
|
|
|
|
Visibility: "public",
|
|
|
|
},
|
2018-12-09 02:19:50 +00:00
|
|
|
{
|
|
|
|
ID: 3,
|
2019-01-24 14:12:17 +00:00
|
|
|
UserName: user3.Name,
|
|
|
|
FullName: user3.FullName,
|
|
|
|
AvatarURL: user3.AvatarLink(),
|
2018-12-09 02:19:50 +00:00
|
|
|
Description: "",
|
|
|
|
Website: "",
|
|
|
|
Location: "",
|
2019-05-30 17:57:55 +00:00
|
|
|
Visibility: "public",
|
2018-12-09 02:19:50 +00:00
|
|
|
},
|
|
|
|
}, orgs)
|
2021-01-28 21:40:41 +00: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 17:46:10 +00:00
|
|
|
token := ""
|
2021-01-28 21:40:41 +00:00
|
|
|
session := emptyTestSession(t)
|
|
|
|
if len(userDoer) != 0 {
|
|
|
|
session = loginUser(t, userDoer)
|
|
|
|
token = getTokenForLoggedInUser(t, session)
|
|
|
|
}
|
|
|
|
urlStr := fmt.Sprintf("/api/v1/users/%s/orgs?token=%s", userCheck, token)
|
|
|
|
req := NewRequest(t, "GET", urlStr)
|
|
|
|
resp := session.MakeRequest(t, req, http.StatusOK)
|
|
|
|
DecodeJSON(t, resp, &orgs)
|
|
|
|
return orgs
|
2018-12-09 02:19:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestMyOrgs(t *testing.T) {
|
2022-09-02 19:18:23 +00:00
|
|
|
defer tests.PrepareTestEnv(t)()
|
2018-12-09 02:19:50 +00:00
|
|
|
|
2021-01-28 21:40:41 +00:00
|
|
|
session := emptyTestSession(t)
|
|
|
|
req := NewRequest(t, "GET", "/api/v1/user/orgs")
|
2021-11-18 01:33:06 +00:00
|
|
|
session.MakeRequest(t, req, http.StatusUnauthorized)
|
2021-01-28 21:40:41 +00:00
|
|
|
|
2018-12-09 02:19:50 +00:00
|
|
|
normalUsername := "user2"
|
2021-01-28 21:40:41 +00:00
|
|
|
session = loginUser(t, normalUsername)
|
2018-12-09 02:19:50 +00:00
|
|
|
token := getTokenForLoggedInUser(t, session)
|
2021-01-28 21:40:41 +00:00
|
|
|
req = NewRequest(t, "GET", "/api/v1/user/orgs?token="+token)
|
2021-11-18 01:33:06 +00:00
|
|
|
resp := session.MakeRequest(t, req, http.StatusOK)
|
2018-12-09 02:19:50 +00:00
|
|
|
var orgs []*api.Organization
|
|
|
|
DecodeJSON(t, resp, &orgs)
|
2022-08-16 02:22:25 +00:00
|
|
|
user3 := unittest.AssertExistsAndLoadBean(t, &user_model.User{Name: "user3"})
|
2022-08-21 16:24:05 +00:00
|
|
|
user17 := unittest.AssertExistsAndLoadBean(t, &user_model.User{Name: "user17"})
|
2018-12-09 02:19:50 +00:00
|
|
|
|
|
|
|
assert.Equal(t, []*api.Organization{
|
2022-08-21 16:24:05 +00:00
|
|
|
{
|
|
|
|
ID: 17,
|
|
|
|
UserName: user17.Name,
|
|
|
|
FullName: user17.FullName,
|
|
|
|
AvatarURL: user17.AvatarLink(),
|
|
|
|
Description: "",
|
|
|
|
Website: "",
|
|
|
|
Location: "",
|
|
|
|
Visibility: "public",
|
|
|
|
},
|
2018-12-09 02:19:50 +00:00
|
|
|
{
|
|
|
|
ID: 3,
|
2019-01-24 14:12:17 +00:00
|
|
|
UserName: user3.Name,
|
|
|
|
FullName: user3.FullName,
|
|
|
|
AvatarURL: user3.AvatarLink(),
|
2018-12-09 02:19:50 +00:00
|
|
|
Description: "",
|
|
|
|
Website: "",
|
|
|
|
Location: "",
|
2019-05-30 17:57:55 +00:00
|
|
|
Visibility: "public",
|
2018-12-09 02:19:50 +00:00
|
|
|
},
|
|
|
|
}, orgs)
|
|
|
|
}
|