2017-01-08 06:10:53 +03:00
// Copyright 2017 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
import (
"testing"
2017-01-25 05:49:51 +03:00
2017-01-08 06:10:53 +03:00
"github.com/stretchr/testify/assert"
)
func TestAccessLevel ( t * testing . T ) {
assert . NoError ( t , PrepareTestDatabase ( ) )
2018-11-28 14:26:14 +03:00
user2 := AssertExistsAndLoadBean ( t , & User { ID : 2 } ) . ( * User )
user5 := AssertExistsAndLoadBean ( t , & User { ID : 5 } ) . ( * User )
2018-06-21 19:00:13 +03:00
// A public repository owned by User 2
repo1 := AssertExistsAndLoadBean ( t , & Repository { ID : 1 } ) . ( * Repository )
assert . False ( t , repo1 . IsPrivate )
// A private repository owned by Org 3
2018-11-28 14:26:14 +03:00
repo3 := AssertExistsAndLoadBean ( t , & Repository { ID : 3 } ) . ( * Repository )
assert . True ( t , repo3 . IsPrivate )
2017-01-08 06:10:53 +03:00
2018-11-28 14:26:14 +03:00
level , err := AccessLevel ( user2 , repo1 )
2017-01-08 06:10:53 +03:00
assert . NoError ( t , err )
assert . Equal ( t , AccessModeOwner , level )
2018-11-28 14:26:14 +03:00
level , err = AccessLevel ( user2 , repo3 )
2017-01-08 06:10:53 +03:00
assert . NoError ( t , err )
2018-11-28 14:26:14 +03:00
assert . Equal ( t , AccessModeOwner , level )
2017-01-08 06:10:53 +03:00
2018-11-28 14:26:14 +03:00
level , err = AccessLevel ( user5 , repo1 )
2017-01-08 06:10:53 +03:00
assert . NoError ( t , err )
assert . Equal ( t , AccessModeRead , level )
2018-11-28 14:26:14 +03:00
level , err = AccessLevel ( user5 , repo3 )
2017-01-08 06:10:53 +03:00
assert . NoError ( t , err )
assert . Equal ( t , AccessModeNone , level )
}
func TestHasAccess ( t * testing . T ) {
assert . NoError ( t , PrepareTestDatabase ( ) )
2017-01-25 05:49:51 +03:00
user1 := AssertExistsAndLoadBean ( t , & User { ID : 2 } ) . ( * User )
2017-07-17 05:04:43 +03:00
user2 := AssertExistsAndLoadBean ( t , & User { ID : 5 } ) . ( * User )
2018-06-21 19:00:13 +03:00
// A public repository owned by User 2
repo1 := AssertExistsAndLoadBean ( t , & Repository { ID : 1 } ) . ( * Repository )
assert . False ( t , repo1 . IsPrivate )
// A private repository owned by Org 3
repo2 := AssertExistsAndLoadBean ( t , & Repository { ID : 3 } ) . ( * Repository )
assert . True ( t , repo2 . IsPrivate )
2017-01-08 06:10:53 +03:00
2018-11-28 14:26:14 +03:00
has , err := HasAccess ( user1 . ID , repo1 )
assert . NoError ( t , err )
assert . True ( t , has )
2017-01-08 06:10:53 +03:00
2019-07-22 10:11:16 +03:00
_ , err = HasAccess ( user1 . ID , repo2 )
2018-11-28 14:26:14 +03:00
assert . NoError ( t , err )
2017-01-08 06:10:53 +03:00
2019-07-22 10:11:16 +03:00
_ , err = HasAccess ( user2 . ID , repo1 )
2018-11-28 14:26:14 +03:00
assert . NoError ( t , err )
2017-01-08 06:10:53 +03:00
2019-07-22 10:11:16 +03:00
_ , err = HasAccess ( user2 . ID , repo2 )
2018-11-28 14:26:14 +03:00
assert . NoError ( t , err )
2017-01-08 06:10:53 +03:00
}
func TestUser_GetRepositoryAccesses ( t * testing . T ) {
assert . NoError ( t , PrepareTestDatabase ( ) )
2017-01-25 05:49:51 +03:00
user1 := AssertExistsAndLoadBean ( t , & User { ID : 1 } ) . ( * User )
2017-01-08 06:10:53 +03:00
accesses , err := user1 . GetRepositoryAccesses ( )
assert . NoError ( t , err )
assert . Len ( t , accesses , 0 )
}
func TestUser_GetAccessibleRepositories ( t * testing . T ) {
assert . NoError ( t , PrepareTestDatabase ( ) )
2017-01-25 05:49:51 +03:00
user1 := AssertExistsAndLoadBean ( t , & User { ID : 1 } ) . ( * User )
2017-01-08 06:10:53 +03:00
repos , err := user1 . GetAccessibleRepositories ( 0 )
assert . NoError ( t , err )
assert . Len ( t , repos , 0 )
2017-01-25 05:49:51 +03:00
user2 := AssertExistsAndLoadBean ( t , & User { ID : 2 } ) . ( * User )
2017-01-08 06:10:53 +03:00
repos , err = user2 . GetAccessibleRepositories ( 0 )
assert . NoError ( t , err )
assert . Len ( t , repos , 1 )
}
func TestRepository_RecalculateAccesses ( t * testing . T ) {
// test with organization repo
assert . NoError ( t , PrepareTestDatabase ( ) )
2017-01-25 05:49:51 +03:00
repo1 := AssertExistsAndLoadBean ( t , & Repository { ID : 3 } ) . ( * Repository )
2017-01-08 06:10:53 +03:00
assert . NoError ( t , repo1 . GetOwner ( ) )
2017-01-27 19:11:41 +03:00
_ , err := x . Delete ( & Collaboration { UserID : 2 , RepoID : 3 } )
2017-01-08 06:10:53 +03:00
assert . NoError ( t , err )
assert . NoError ( t , repo1 . RecalculateAccesses ( ) )
access := & Access { UserID : 2 , RepoID : 3 }
2017-01-27 19:11:41 +03:00
has , err := x . Get ( access )
2017-01-08 06:10:53 +03:00
assert . NoError ( t , err )
assert . True ( t , has )
2017-01-24 19:16:36 +03:00
assert . Equal ( t , AccessModeOwner , access . Mode )
2017-01-08 06:10:53 +03:00
}
func TestRepository_RecalculateAccesses2 ( t * testing . T ) {
// test with non-organization repo
assert . NoError ( t , PrepareTestDatabase ( ) )
2017-01-25 05:49:51 +03:00
repo1 := AssertExistsAndLoadBean ( t , & Repository { ID : 4 } ) . ( * Repository )
2017-01-08 06:10:53 +03:00
assert . NoError ( t , repo1 . GetOwner ( ) )
2017-01-27 19:11:41 +03:00
_ , err := x . Delete ( & Collaboration { UserID : 4 , RepoID : 4 } )
2017-01-08 06:10:53 +03:00
assert . NoError ( t , err )
assert . NoError ( t , repo1 . RecalculateAccesses ( ) )
2017-01-27 19:11:41 +03:00
has , err := x . Get ( & Access { UserID : 4 , RepoID : 4 } )
2017-01-08 06:10:53 +03:00
assert . NoError ( t , err )
assert . False ( t , has )
}