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