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