Unit tests for models/access.go (#606)

tokarchuk/v1.17
Ethan Koenig 8 years ago committed by Lunny Xiao
parent 8422ab542c
commit 6072b03291
  1. 136
      models/access_test.go
  2. 11
      models/fixtures/access.yml
  3. 11
      models/fixtures/collaboration.yml
  4. 7
      models/fixtures/org_user.yml
  5. 22
      models/fixtures/repository.yml
  6. 7
      models/fixtures/team.yml
  7. 5
      models/fixtures/team_repo.yml
  8. 5
      models/fixtures/team_user.yml
  9. 39
      models/fixtures/user.yml
  10. 15
      models/setup_for_test.go

@ -0,0 +1,136 @@
// 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"
"github.com/stretchr/testify/assert"
)
var accessModes = []AccessMode{
AccessModeRead,
AccessModeWrite,
AccessModeAdmin,
AccessModeOwner,
}
func TestAccessLevel(t *testing.T) {
assert.NoError(t, PrepareTestDatabase())
user1 := &User{ID: 2}; LoadTestFixture(t, user1)
user2 := &User{ID: 4}; LoadTestFixture(t, user2)
repo1 := &Repository{OwnerID: 2, IsPrivate: false}; LoadTestFixture(t, repo1)
repo2 := &Repository{OwnerID: 3, IsPrivate: true}; LoadTestFixture(t, repo2)
level, err := AccessLevel(user1, repo1)
assert.NoError(t, err)
assert.Equal(t, AccessModeOwner, level)
level, err = AccessLevel(user1, repo2)
assert.NoError(t, err)
assert.Equal(t, AccessModeWrite, level)
level, err = AccessLevel(user2, repo1)
assert.NoError(t, err)
assert.Equal(t, AccessModeRead, level)
level, err = AccessLevel(user2, repo2)
assert.NoError(t, err)
assert.Equal(t, AccessModeNone, level)
}
func TestHasAccess(t *testing.T) {
assert.NoError(t, PrepareTestDatabase())
user1 := &User{ID: 2}; LoadTestFixture(t, user1)
user2 := &User{ID: 4}; LoadTestFixture(t, user2)
repo1 := &Repository{OwnerID: 2, IsPrivate: false}; LoadTestFixture(t, repo1)
repo2 := &Repository{OwnerID: 3, IsPrivate: true}; LoadTestFixture(t, repo2)
for _, accessMode := range accessModes {
has, err := HasAccess(user1, repo1, accessMode)
assert.NoError(t, err)
assert.True(t, has)
has, err = HasAccess(user1, repo2, accessMode)
assert.NoError(t, err)
assert.Equal(t, accessMode <= AccessModeWrite, has)
has, err = HasAccess(user2, repo1, accessMode)
assert.NoError(t, err)
assert.Equal(t, accessMode <= AccessModeRead, has)
has, err = HasAccess(user2, repo2, accessMode)
assert.NoError(t, err)
assert.Equal(t, accessMode <= AccessModeNone, has)
}
}
func TestUser_GetRepositoryAccesses(t *testing.T) {
assert.NoError(t, PrepareTestDatabase())
user1 := &User{ID: 1}; LoadTestFixture(t, user1)
user2 := &User{ID: 2}; LoadTestFixture(t, user2)
accesses, err := user1.GetRepositoryAccesses()
assert.NoError(t, err)
assert.Len(t, accesses, 0)
}
func TestUser_GetAccessibleRepositories(t *testing.T) {
assert.NoError(t, PrepareTestDatabase())
user1 := &User{ID: 1}; LoadTestFixture(t, user1)
user2 := &User{ID: 2}; LoadTestFixture(t, user2)
repos, err := user1.GetAccessibleRepositories(0)
assert.NoError(t, err)
assert.Len(t, repos, 0)
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())
repo1 := &Repository{ID: 3}; LoadTestFixture(t, repo1)
assert.NoError(t, repo1.GetOwner())
sess := x.NewSession()
defer sess.Close()
_, err := sess.Delete(&Collaboration{UserID: 2, RepoID: 3})
assert.NoError(t, err)
assert.NoError(t, repo1.RecalculateAccesses())
sess = x.NewSession()
access := &Access{UserID: 2, RepoID: 3}
has, err := sess.Get(access)
assert.NoError(t, err)
assert.True(t, has)
assert.Equal(t, AccessModeWrite, access.Mode)
}
func TestRepository_RecalculateAccesses2(t *testing.T) {
// test with non-organization repo
assert.NoError(t, PrepareTestDatabase())
repo1 := &Repository{ID: 4}; LoadTestFixture(t, repo1)
assert.NoError(t, repo1.GetOwner())
sess := x.NewSession()
defer sess.Close()
_, err := sess.Delete(&Collaboration{UserID: 4, RepoID: 4})
assert.NoError(t, err)
assert.NoError(t, repo1.RecalculateAccesses())
sess = x.NewSession()
has, err := sess.Get(&Access{UserID: 4, RepoID: 4})
assert.NoError(t, err)
assert.False(t, has)
}

@ -0,0 +1,11 @@
-
id: 1
user_id: 2
repo_id: 3
mode: 2 # write
-
id: 2
repo_id: 4
user_id: 4
mode: 2 # write

@ -0,0 +1,11 @@
-
id: 1
repo_id: 3
user_id: 2
mode: 2 # write
-
id: 2
repo_id: 4
user_id: 4
mode: 2 # write

@ -0,0 +1,7 @@
-
id: 1
uid: 2
org_id: 3
is_public: true
is_owner: true
num_teams: 1

@ -19,3 +19,25 @@
num_closed_issues: 1 num_closed_issues: 1
num_pulls: 0 num_pulls: 0
num_closed_pulls: 0 num_closed_pulls: 0
-
id: 3
owner_id: 3
lower_name: repo3
name: repo3
is_private: true
num_issues: 0
num_closed_issues: 0
num_pulls: 0
num_closed_pulls: 0
-
id: 4
owner_id: 5
lower_name: repo4
name: repo4
is_private: false
num_issues: 0
num_closed_issues: 0
num_pulls: 0
num_closed_pulls: 0

@ -0,0 +1,7 @@
-
id: 1
org_id: 3
lower_name: team1
name: team1
authorize: 2 # write
num_repos: 1

@ -0,0 +1,5 @@
-
id: 1
org_id: 3
team_id: 1
repo_id: 3

@ -0,0 +1,5 @@
-
id: 1
org_id: 3
team_id: 1
uid: 2

@ -23,3 +23,42 @@
avatar: avatar2 avatar: avatar2
avatar_email: user2@example.com avatar_email: user2@example.com
num_repos: 2 num_repos: 2
-
id: 3
lower_name: user3
name: user3
email: user3@example.com
passwd: password
type: 1 # organization
salt: salt
is_admin: false
avatar: avatar3
avatar_email: user3@example.com
num_repos: 1
-
id: 4
lower_name: user4
name: user4
email: user4@example.com
passwd: password
type: 1 # individual
salt: salt
is_admin: false
avatar: avatar4
avatar_email: user4@example.com
num_repos: 0
-
id: 5
lower_name: user5
name: user5
email: user5@example.com
passwd: password
type: 1 # individual
salt: salt
is_admin: false
avatar: avatar5
avatar_email: user5@example.com
num_repos: 1

@ -12,6 +12,7 @@ import (
"github.com/go-xorm/core" "github.com/go-xorm/core"
"github.com/go-xorm/xorm" "github.com/go-xorm/xorm"
_ "github.com/mattn/go-sqlite3" // for the test engine _ "github.com/mattn/go-sqlite3" // for the test engine
"github.com/stretchr/testify/assert"
"gopkg.in/testfixtures.v2" "gopkg.in/testfixtures.v2"
) )
@ -45,3 +46,17 @@ func CreateTestEngine() error {
func PrepareTestDatabase() error { func PrepareTestDatabase() error {
return fixtures.Load() return fixtures.Load()
} }
// LoadFixture load a test fixture from the test database, failing if fixture
// does not exist
func LoadTestFixture(t *testing.T, fixture interface{}, conditions... interface{}) {
sess := x.NewSession()
defer sess.Close()
for _, cond := range conditions {
sess = sess.Where(cond)
}
has, err := sess.Get(fixture)
assert.NoError(t, err)
assert.True(t, has)
}

Loading…
Cancel
Save