Files
Atay-Makhzan/tests/integration/api_user_orgs_test.go
T

199 lines
5.5 KiB
Go
Raw Normal View History

2018-12-09 10:19:50 +08:00
// Copyright 2018 The Gitea Authors. All rights reserved.
// SPDX-License-Identifier: MIT
2018-12-09 10:19:50 +08:00
2022-09-02 15:18:23 -04:00
package integration
2018-12-09 10:19:50 +08:00
import (
"fmt"
"net/http"
"testing"
2023-01-17 16:46:03 -05:00
auth_model "code.gitea.io/gitea/models/auth"
"code.gitea.io/gitea/models/unittest"
user_model "code.gitea.io/gitea/models/user"
2019-05-11 18:21:34 +08:00
api "code.gitea.io/gitea/modules/structs"
2022-09-02 15:18:23 -04:00
"code.gitea.io/gitea/tests"
2018-12-09 10:19:50 +08:00
"github.com/stretchr/testify/assert"
)
func TestUserOrgs(t *testing.T) {
2022-09-02 15:18:23 -04:00
defer tests.PrepareTestEnv(t)()
2018-12-09 10:19:50 +08:00
adminUsername := "user1"
normalUsername := "user2"
privateMemberUsername := "user4"
unrelatedUsername := "user5"
orgs := getUserOrgs(t, adminUsername, normalUsername)
org3 := unittest.AssertExistsAndLoadBean(t, &user_model.User{Name: "org3"})
org17 := unittest.AssertExistsAndLoadBean(t, &user_model.User{Name: "org17"})
org35 := unittest.AssertExistsAndLoadBean(t, &user_model.User{Name: "private_org35"})
2018-12-09 10:19:50 +08:00
assert.Equal(t, []*api.Organization{
2022-08-21 18:24:05 +02:00
{
ID: 17,
Name: org17.Name,
UserName: org17.Name,
FullName: org17.FullName,
Email: org17.Email,
AvatarURL: org17.AvatarLink(t.Context()),
2022-08-21 18:24:05 +02:00
Description: "",
Website: "",
Location: "",
Visibility: "public",
},
2018-12-09 10:19:50 +08:00
{
ID: 3,
Name: org3.Name,
UserName: org3.Name,
FullName: org3.FullName,
Email: org3.Email,
AvatarURL: org3.AvatarLink(t.Context()),
2018-12-09 10:19:50 +08:00
Description: "",
Website: "",
Location: "",
2019-05-30 13:57:55 -04:00
Visibility: "public",
2018-12-09 10:19:50 +08:00
},
{
ID: 35,
Name: org35.Name,
UserName: org35.Name,
FullName: org35.FullName,
Email: org35.Email,
AvatarURL: org35.AvatarLink(t.Context()),
Description: "",
Website: "",
Location: "",
Visibility: "private",
},
2018-12-09 10:19:50 +08:00
}, orgs)
// 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)
2024-12-15 11:41:29 +01:00
assert.Empty(t, orgs)
2023-01-17 16:46:03 -05:00
// not authenticated call should not be allowed
testUserOrgsUnauthenticated(t, privateMemberUsername)
}
func getUserOrgs(t *testing.T, userDoer, userCheck string) (orgs []*api.Organization) {
2022-01-20 18:46:10 +01:00
token := ""
if len(userDoer) != 0 {
2023-06-04 14:57:16 -04:00
token = getUserToken(t, userDoer, auth_model.AccessTokenScopeReadOrganization, auth_model.AccessTokenScopeReadUser)
}
req := NewRequest(t, "GET", fmt.Sprintf("/api/v1/users/%s/orgs", userCheck)).
AddTokenAuth(token)
2022-12-02 11:39:42 +08:00
resp := MakeRequest(t, req, http.StatusOK)
DecodeJSON(t, resp, &orgs)
return orgs
2018-12-09 10:19:50 +08:00
}
2023-01-17 16:46:03 -05:00
func testUserOrgsUnauthenticated(t *testing.T, userCheck string) {
session := emptyTestSession(t)
req := NewRequestf(t, "GET", "/api/v1/users/%s/orgs", userCheck)
session.MakeRequest(t, req, http.StatusUnauthorized)
}
2018-12-09 10:19:50 +08:00
func TestMyOrgs(t *testing.T) {
2022-09-02 15:18:23 -04:00
defer tests.PrepareTestEnv(t)()
2018-12-09 10:19:50 +08:00
req := NewRequest(t, "GET", "/api/v1/user/orgs")
2022-12-02 11:39:42 +08:00
MakeRequest(t, req, http.StatusUnauthorized)
2018-12-09 10:19:50 +08:00
normalUsername := "user2"
2023-06-04 14:57:16 -04:00
token := getUserToken(t, normalUsername, auth_model.AccessTokenScopeReadOrganization, auth_model.AccessTokenScopeReadUser)
req = NewRequest(t, "GET", "/api/v1/user/orgs").
AddTokenAuth(token)
2022-12-02 11:39:42 +08:00
resp := MakeRequest(t, req, http.StatusOK)
2018-12-09 10:19:50 +08:00
var orgs []*api.Organization
DecodeJSON(t, resp, &orgs)
org3 := unittest.AssertExistsAndLoadBean(t, &user_model.User{Name: "org3"})
org17 := unittest.AssertExistsAndLoadBean(t, &user_model.User{Name: "org17"})
org35 := unittest.AssertExistsAndLoadBean(t, &user_model.User{Name: "private_org35"})
2018-12-09 10:19:50 +08:00
assert.Equal(t, []*api.Organization{
2022-08-21 18:24:05 +02:00
{
ID: 17,
Name: org17.Name,
UserName: org17.Name,
FullName: org17.FullName,
Email: org17.Email,
AvatarURL: org17.AvatarLink(t.Context()),
2022-08-21 18:24:05 +02:00
Description: "",
Website: "",
Location: "",
Visibility: "public",
},
2018-12-09 10:19:50 +08:00
{
ID: 3,
Name: org3.Name,
UserName: org3.Name,
FullName: org3.FullName,
Email: org3.Email,
AvatarURL: org3.AvatarLink(t.Context()),
2018-12-09 10:19:50 +08:00
Description: "",
Website: "",
Location: "",
2019-05-30 13:57:55 -04:00
Visibility: "public",
2018-12-09 10:19:50 +08:00
},
{
ID: 35,
Name: org35.Name,
UserName: org35.Name,
FullName: org35.FullName,
Email: org35.Email,
AvatarURL: org35.AvatarLink(t.Context()),
Description: "",
Website: "",
Location: "",
Visibility: "private",
},
2018-12-09 10:19:50 +08:00
}, orgs)
}
func TestMyOrgsPublicOnly(t *testing.T) {
defer tests.PrepareTestEnv(t)()
normalUsername := "user2"
token := getUserToken(t, normalUsername, auth_model.AccessTokenScopeReadOrganization, auth_model.AccessTokenScopeReadUser, auth_model.AccessTokenScopePublicOnly)
req := NewRequest(t, "GET", "/api/v1/user/orgs").
AddTokenAuth(token)
resp := MakeRequest(t, req, http.StatusOK)
var orgs []*api.Organization
DecodeJSON(t, resp, &orgs)
org3 := unittest.AssertExistsAndLoadBean(t, &user_model.User{Name: "org3"})
org17 := unittest.AssertExistsAndLoadBean(t, &user_model.User{Name: "org17"})
assert.Equal(t, []*api.Organization{
{
ID: 17,
Name: org17.Name,
UserName: org17.Name,
FullName: org17.FullName,
Email: org17.Email,
AvatarURL: org17.AvatarLink(t.Context()),
Description: "",
Website: "",
Location: "",
Visibility: "public",
},
{
ID: 3,
Name: org3.Name,
UserName: org3.Name,
FullName: org3.FullName,
Email: org3.Email,
AvatarURL: org3.AvatarLink(t.Context()),
Description: "",
Website: "",
Location: "",
Visibility: "public",
},
}, orgs)
}