2018-11-20 20:31:30 +03:00
// Copyright 2018 The Gitea Authors. All rights reserved.
2022-11-27 21:20:29 +03:00
// SPDX-License-Identifier: MIT
2018-11-20 20:31:30 +03:00
2022-09-02 22:18:23 +03:00
package integration
2018-11-20 20:31:30 +03:00
import (
2022-09-19 15:02:29 +03:00
"fmt"
2018-11-20 20:31:30 +03:00
"net/http"
2019-04-24 19:16:14 +03:00
"net/url"
2018-11-20 20:31:30 +03:00
"strings"
"testing"
2023-01-18 00:46:03 +03:00
auth_model "code.gitea.io/gitea/models/auth"
2023-04-20 02:19:13 +03:00
"code.gitea.io/gitea/models/db"
org_model "code.gitea.io/gitea/models/organization"
"code.gitea.io/gitea/models/perm"
unit_model "code.gitea.io/gitea/models/unit"
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-04-24 19:16:14 +03:00
"code.gitea.io/gitea/modules/setting"
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"
2018-11-20 20:31:30 +03:00
"github.com/stretchr/testify/assert"
)
2019-05-30 20:57:55 +03:00
func TestAPIOrgCreate ( t * testing . T ) {
2019-04-24 19:16:14 +03:00
onGiteaRun ( t , func ( * testing . T , * url . URL ) {
2023-01-18 00:46:03 +03:00
token := getUserToken ( t , "user1" , auth_model . AccessTokenScopeWriteOrg )
2019-04-24 19:16:14 +03:00
2022-01-20 20:46:10 +03:00
org := api . CreateOrgOption {
2019-04-24 19:16:14 +03:00
UserName : "user1_org" ,
FullName : "User1's organization" ,
Description : "This organization created by user1" ,
Website : "https://try.gitea.io" ,
Location : "Shanghai" ,
2019-05-30 20:57:55 +03:00
Visibility : "limited" ,
2019-04-24 19:16:14 +03:00
}
req := NewRequestWithJSON ( t , "POST" , "/api/v1/orgs?token=" + token , & org )
2022-04-08 07:22:10 +03:00
resp := MakeRequest ( t , req , http . StatusCreated )
2019-04-24 19:16:14 +03:00
var apiOrg api . Organization
DecodeJSON ( t , resp , & apiOrg )
2022-09-29 06:27:33 +03:00
assert . Equal ( t , org . UserName , apiOrg . Name )
2019-04-24 19:16:14 +03:00
assert . Equal ( t , org . FullName , apiOrg . FullName )
assert . Equal ( t , org . Description , apiOrg . Description )
assert . Equal ( t , org . Website , apiOrg . Website )
assert . Equal ( t , org . Location , apiOrg . Location )
2019-05-30 20:57:55 +03:00
assert . Equal ( t , org . Visibility , apiOrg . Visibility )
2019-04-24 19:16:14 +03:00
2021-11-24 12:49:20 +03:00
unittest . AssertExistsAndLoadBean ( t , & user_model . User {
2019-04-24 19:16:14 +03:00
Name : org . UserName ,
LowerName : strings . ToLower ( org . UserName ) ,
FullName : org . FullName ,
} )
2023-04-20 02:19:13 +03:00
// Check owner team permission
ownerTeam , _ := org_model . GetOwnerTeam ( db . DefaultContext , apiOrg . ID )
for _ , ut := range unit_model . AllRepoUnitTypes {
up := perm . AccessModeOwner
if ut == unit_model . TypeExternalTracker || ut == unit_model . TypeExternalWiki {
up = perm . AccessModeRead
}
unittest . AssertExistsAndLoadBean ( t , & org_model . TeamUnit {
OrgID : apiOrg . ID ,
TeamID : ownerTeam . ID ,
Type : ut ,
AccessMode : up ,
} )
}
2022-04-08 07:22:10 +03:00
req = NewRequestf ( t , "GET" , "/api/v1/orgs/%s?token=%s" , org . UserName , token )
resp = MakeRequest ( t , req , http . StatusOK )
2019-04-24 19:16:14 +03:00
DecodeJSON ( t , resp , & apiOrg )
2022-09-29 06:27:33 +03:00
assert . EqualValues ( t , org . UserName , apiOrg . Name )
2019-04-24 19:16:14 +03:00
2022-04-08 07:22:10 +03:00
req = NewRequestf ( t , "GET" , "/api/v1/orgs/%s/repos?token=%s" , org . UserName , token )
resp = MakeRequest ( t , req , http . StatusOK )
2019-04-24 19:16:14 +03:00
var repos [ ] * api . Repository
DecodeJSON ( t , resp , & repos )
for _ , repo := range repos {
assert . False ( t , repo . Private )
}
2022-04-08 07:22:10 +03:00
req = NewRequestf ( t , "GET" , "/api/v1/orgs/%s/members?token=%s" , org . UserName , token )
resp = MakeRequest ( t , req , http . StatusOK )
2019-04-24 19:16:14 +03:00
// user1 on this org is public
var users [ ] * api . User
DecodeJSON ( t , resp , & users )
2021-06-07 08:27:09 +03:00
assert . Len ( t , users , 1 )
2019-04-24 19:16:14 +03:00
assert . EqualValues ( t , "user1" , users [ 0 ] . UserName )
} )
}
2019-05-30 20:57:55 +03:00
func TestAPIOrgEdit ( t * testing . T ) {
onGiteaRun ( t , func ( * testing . T , * url . URL ) {
session := loginUser ( t , "user1" )
2023-01-18 00:46:03 +03:00
token := getTokenForLoggedInUser ( t , session , auth_model . AccessTokenScopeWriteOrg )
2022-01-20 20:46:10 +03:00
org := api . EditOrgOption {
2019-05-30 20:57:55 +03:00
FullName : "User3 organization new full name" ,
Description : "A new description" ,
Website : "https://try.gitea.io/new" ,
Location : "Beijing" ,
Visibility : "private" ,
}
req := NewRequestWithJSON ( t , "PATCH" , "/api/v1/orgs/user3?token=" + token , & org )
2022-12-02 06:39:42 +03:00
resp := MakeRequest ( t , req , http . StatusOK )
2019-05-30 20:57:55 +03:00
var apiOrg api . Organization
DecodeJSON ( t , resp , & apiOrg )
2022-09-29 06:27:33 +03:00
assert . Equal ( t , "user3" , apiOrg . Name )
2019-05-30 20:57:55 +03:00
assert . Equal ( t , org . FullName , apiOrg . FullName )
assert . Equal ( t , org . Description , apiOrg . Description )
assert . Equal ( t , org . Website , apiOrg . Website )
assert . Equal ( t , org . Location , apiOrg . Location )
assert . Equal ( t , org . Visibility , apiOrg . Visibility )
} )
}
func TestAPIOrgEditBadVisibility ( t * testing . T ) {
onGiteaRun ( t , func ( * testing . T , * url . URL ) {
session := loginUser ( t , "user1" )
2023-01-18 00:46:03 +03:00
token := getTokenForLoggedInUser ( t , session , auth_model . AccessTokenScopeWriteOrg )
2022-01-20 20:46:10 +03:00
org := api . EditOrgOption {
2019-05-30 20:57:55 +03:00
FullName : "User3 organization new full name" ,
Description : "A new description" ,
Website : "https://try.gitea.io/new" ,
Location : "Beijing" ,
Visibility : "badvisibility" ,
}
req := NewRequestWithJSON ( t , "PATCH" , "/api/v1/orgs/user3?token=" + token , & org )
2022-12-02 06:39:42 +03:00
MakeRequest ( t , req , http . StatusUnprocessableEntity )
2019-05-30 20:57:55 +03:00
} )
}
2019-04-24 19:16:14 +03:00
func TestAPIOrgDeny ( t * testing . T ) {
onGiteaRun ( t , func ( * testing . T , * url . URL ) {
setting . Service . RequireSignInView = true
defer func ( ) {
setting . Service . RequireSignInView = false
} ( )
2022-01-20 20:46:10 +03:00
orgName := "user1_org"
2023-04-21 18:39:03 +03:00
req := NewRequestf ( t , "GET" , "/api/v1/orgs/%s" , orgName )
2019-04-24 19:16:14 +03:00
MakeRequest ( t , req , http . StatusNotFound )
2023-04-21 18:39:03 +03:00
req = NewRequestf ( t , "GET" , "/api/v1/orgs/%s/repos" , orgName )
2019-04-24 19:16:14 +03:00
MakeRequest ( t , req , http . StatusNotFound )
2023-04-21 18:39:03 +03:00
req = NewRequestf ( t , "GET" , "/api/v1/orgs/%s/members" , orgName )
2019-04-24 19:16:14 +03:00
MakeRequest ( t , req , http . StatusNotFound )
2018-11-20 20:31:30 +03:00
} )
}
2020-01-12 18:43:44 +03:00
func TestAPIGetAll ( t * testing . T ) {
2022-09-02 22:18:23 +03:00
defer tests . PrepareTestEnv ( t ) ( )
2020-01-12 18:43:44 +03:00
2023-01-18 00:46:03 +03:00
token := getUserToken ( t , "user1" , auth_model . AccessTokenScopeReadOrg )
2023-04-21 18:39:03 +03:00
// accessing with a token will return all orgs
2023-01-18 00:46:03 +03:00
req := NewRequestf ( t , "GET" , "/api/v1/orgs?token=%s" , token )
2020-01-12 18:43:44 +03:00
resp := MakeRequest ( t , req , http . StatusOK )
var apiOrgList [ ] * api . Organization
2023-04-21 18:39:03 +03:00
DecodeJSON ( t , resp , & apiOrgList )
2023-01-18 00:46:03 +03:00
assert . Len ( t , apiOrgList , 9 )
assert . Equal ( t , "org25" , apiOrgList [ 1 ] . FullName )
assert . Equal ( t , "public" , apiOrgList [ 1 ] . Visibility )
2023-04-21 18:39:03 +03:00
// accessing without a token will return only public orgs
req = NewRequestf ( t , "GET" , "/api/v1/orgs" )
resp = MakeRequest ( t , req , http . StatusOK )
DecodeJSON ( t , resp , & apiOrgList )
assert . Len ( t , apiOrgList , 7 )
assert . Equal ( t , "org25" , apiOrgList [ 0 ] . FullName )
assert . Equal ( t , "public" , apiOrgList [ 0 ] . Visibility )
2020-01-12 18:43:44 +03:00
}
2022-09-19 15:02:29 +03:00
func TestAPIOrgSearchEmptyTeam ( t * testing . T ) {
onGiteaRun ( t , func ( * testing . T , * url . URL ) {
2023-01-18 00:46:03 +03:00
token := getUserToken ( t , "user1" , auth_model . AccessTokenScopeAdminOrg )
2022-09-19 15:02:29 +03:00
orgName := "org_with_empty_team"
// create org
req := NewRequestWithJSON ( t , "POST" , "/api/v1/orgs?token=" + token , & api . CreateOrgOption {
UserName : orgName ,
} )
MakeRequest ( t , req , http . StatusCreated )
// create team with no member
req = NewRequestWithJSON ( t , "POST" , fmt . Sprintf ( "/api/v1/orgs/%s/teams?token=%s" , orgName , token ) , & api . CreateTeamOption {
Name : "Empty" ,
IncludesAllRepositories : true ,
Permission : "read" ,
Units : [ ] string { "repo.code" , "repo.issues" , "repo.ext_issues" , "repo.wiki" , "repo.pulls" } ,
} )
MakeRequest ( t , req , http . StatusCreated )
// case-insensitive search for teams that have no members
req = NewRequest ( t , "GET" , fmt . Sprintf ( "/api/v1/orgs/%s/teams/search?q=%s&token=%s" , orgName , "empty" , token ) )
resp := MakeRequest ( t , req , http . StatusOK )
data := struct {
Ok bool
Data [ ] * api . Team
} { }
DecodeJSON ( t , resp , & data )
assert . True ( t , data . Ok )
if assert . Len ( t , data . Data , 1 ) {
assert . EqualValues ( t , "Empty" , data . Data [ 0 ] . Name )
}
} )
}