2018-12-08 21:19:50 -05: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
|
|
|
|
|
|
|
|
package integrations
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"net/http"
|
|
|
|
"testing"
|
|
|
|
|
2021-11-16 03:53:21 -05:00
|
|
|
"code.gitea.io/gitea/models/unittest"
|
2021-11-24 04:49:20 -05:00
|
|
|
user_model "code.gitea.io/gitea/models/user"
|
2019-05-11 06:21:34 -04:00
|
|
|
api "code.gitea.io/gitea/modules/structs"
|
2019-01-24 09:12:17 -05:00
|
|
|
|
2018-12-08 21:19:50 -05:00
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestUserOrgs(t *testing.T) {
|
2019-11-25 18:21:37 -05:00
|
|
|
defer prepareTestEnv(t)()
|
2018-12-08 21:19:50 -05:00
|
|
|
adminUsername := "user1"
|
|
|
|
normalUsername := "user2"
|
2021-01-28 16:40:41 -05:00
|
|
|
privateMemberUsername := "user4"
|
|
|
|
unrelatedUsername := "user5"
|
2019-01-24 09:12:17 -05:00
|
|
|
|
2021-01-28 16:40:41 -05:00
|
|
|
orgs := getUserOrgs(t, adminUsername, normalUsername)
|
|
|
|
|
2022-08-15 22:22:25 -04:00
|
|
|
user3 := unittest.AssertExistsAndLoadBean(t, &user_model.User{Name: "user3"})
|
2022-08-21 12:24:05 -04:00
|
|
|
user17 := unittest.AssertExistsAndLoadBean(t, &user_model.User{Name: "user17"})
|
2018-12-08 21:19:50 -05:00
|
|
|
|
|
|
|
assert.Equal(t, []*api.Organization{
|
2022-08-21 12:24:05 -04:00
|
|
|
{
|
|
|
|
ID: 17,
|
|
|
|
UserName: user17.Name,
|
|
|
|
FullName: user17.FullName,
|
|
|
|
AvatarURL: user17.AvatarLink(),
|
|
|
|
Description: "",
|
|
|
|
Website: "",
|
|
|
|
Location: "",
|
|
|
|
Visibility: "public",
|
|
|
|
},
|
2018-12-08 21:19:50 -05:00
|
|
|
{
|
|
|
|
ID: 3,
|
2019-01-24 09:12:17 -05:00
|
|
|
UserName: user3.Name,
|
|
|
|
FullName: user3.FullName,
|
|
|
|
AvatarURL: user3.AvatarLink(),
|
2018-12-08 21:19:50 -05:00
|
|
|
Description: "",
|
|
|
|
Website: "",
|
|
|
|
Location: "",
|
2019-05-30 13:57:55 -04:00
|
|
|
Visibility: "public",
|
2018-12-08 21:19:50 -05:00
|
|
|
},
|
|
|
|
}, orgs)
|
2021-01-28 16:40:41 -05: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 12:46:10 -05:00
|
|
|
token := ""
|
2021-01-28 16:40:41 -05: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-08 21:19:50 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestMyOrgs(t *testing.T) {
|
2019-11-25 18:21:37 -05:00
|
|
|
defer prepareTestEnv(t)()
|
2018-12-08 21:19:50 -05:00
|
|
|
|
2021-01-28 16:40:41 -05:00
|
|
|
session := emptyTestSession(t)
|
|
|
|
req := NewRequest(t, "GET", "/api/v1/user/orgs")
|
2021-11-17 20:33:06 -05:00
|
|
|
session.MakeRequest(t, req, http.StatusUnauthorized)
|
2021-01-28 16:40:41 -05:00
|
|
|
|
2018-12-08 21:19:50 -05:00
|
|
|
normalUsername := "user2"
|
2021-01-28 16:40:41 -05:00
|
|
|
session = loginUser(t, normalUsername)
|
2018-12-08 21:19:50 -05:00
|
|
|
token := getTokenForLoggedInUser(t, session)
|
2021-01-28 16:40:41 -05:00
|
|
|
req = NewRequest(t, "GET", "/api/v1/user/orgs?token="+token)
|
2021-11-17 20:33:06 -05:00
|
|
|
resp := session.MakeRequest(t, req, http.StatusOK)
|
2018-12-08 21:19:50 -05:00
|
|
|
var orgs []*api.Organization
|
|
|
|
DecodeJSON(t, resp, &orgs)
|
2022-08-15 22:22:25 -04:00
|
|
|
user3 := unittest.AssertExistsAndLoadBean(t, &user_model.User{Name: "user3"})
|
2022-08-21 12:24:05 -04:00
|
|
|
user17 := unittest.AssertExistsAndLoadBean(t, &user_model.User{Name: "user17"})
|
2018-12-08 21:19:50 -05:00
|
|
|
|
|
|
|
assert.Equal(t, []*api.Organization{
|
2022-08-21 12:24:05 -04:00
|
|
|
{
|
|
|
|
ID: 17,
|
|
|
|
UserName: user17.Name,
|
|
|
|
FullName: user17.FullName,
|
|
|
|
AvatarURL: user17.AvatarLink(),
|
|
|
|
Description: "",
|
|
|
|
Website: "",
|
|
|
|
Location: "",
|
|
|
|
Visibility: "public",
|
|
|
|
},
|
2018-12-08 21:19:50 -05:00
|
|
|
{
|
|
|
|
ID: 3,
|
2019-01-24 09:12:17 -05:00
|
|
|
UserName: user3.Name,
|
|
|
|
FullName: user3.FullName,
|
|
|
|
AvatarURL: user3.AvatarLink(),
|
2018-12-08 21:19:50 -05:00
|
|
|
Description: "",
|
|
|
|
Website: "",
|
|
|
|
Location: "",
|
2019-05-30 13:57:55 -04:00
|
|
|
Visibility: "public",
|
2018-12-08 21:19:50 -05:00
|
|
|
},
|
|
|
|
}, orgs)
|
|
|
|
}
|