summaryrefslogtreecommitdiffstats
path: root/models
diff options
context:
space:
mode:
authorEthan Koenig <etk39@cornell.edu>2017-01-07 22:10:53 -0500
committerLunny Xiao <xiaolunwen@gmail.com>2017-01-08 11:10:53 +0800
commit6072b032918006824f260e79f96372dd94fbb17f (patch)
tree16e9ee4c0f0550ec55dd2463496649d44e2dfe99 /models
parent8422ab542c69592b4629188d4460ca560307465b (diff)
downloadgitea-6072b032918006824f260e79f96372dd94fbb17f.tar.gz
gitea-6072b032918006824f260e79f96372dd94fbb17f.zip
Unit tests for models/access.go (#606)
Diffstat (limited to 'models')
-rw-r--r--models/access_test.go136
-rw-r--r--models/fixtures/access.yml11
-rw-r--r--models/fixtures/collaboration.yml11
-rw-r--r--models/fixtures/org_user.yml7
-rw-r--r--models/fixtures/repository.yml22
-rw-r--r--models/fixtures/team.yml7
-rw-r--r--models/fixtures/team_repo.yml5
-rw-r--r--models/fixtures/team_user.yml5
-rw-r--r--models/fixtures/user.yml39
-rw-r--r--models/setup_for_test.go15
10 files changed, 258 insertions, 0 deletions
diff --git a/models/access_test.go b/models/access_test.go
new file mode 100644
index 0000000000..c9847f0069
--- /dev/null
+++ b/models/access_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)
+}
diff --git a/models/fixtures/access.yml b/models/fixtures/access.yml
new file mode 100644
index 0000000000..f38081c993
--- /dev/null
+++ b/models/fixtures/access.yml
@@ -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
diff --git a/models/fixtures/collaboration.yml b/models/fixtures/collaboration.yml
new file mode 100644
index 0000000000..18db9c36c5
--- /dev/null
+++ b/models/fixtures/collaboration.yml
@@ -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
diff --git a/models/fixtures/org_user.yml b/models/fixtures/org_user.yml
new file mode 100644
index 0000000000..b4df3c0127
--- /dev/null
+++ b/models/fixtures/org_user.yml
@@ -0,0 +1,7 @@
+-
+ id: 1
+ uid: 2
+ org_id: 3
+ is_public: true
+ is_owner: true
+ num_teams: 1
diff --git a/models/fixtures/repository.yml b/models/fixtures/repository.yml
index 21d9184d13..00b0077283 100644
--- a/models/fixtures/repository.yml
+++ b/models/fixtures/repository.yml
@@ -19,3 +19,25 @@
num_closed_issues: 1
num_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
diff --git a/models/fixtures/team.yml b/models/fixtures/team.yml
new file mode 100644
index 0000000000..9746f50d81
--- /dev/null
+++ b/models/fixtures/team.yml
@@ -0,0 +1,7 @@
+-
+ id: 1
+ org_id: 3
+ lower_name: team1
+ name: team1
+ authorize: 2 # write
+ num_repos: 1
diff --git a/models/fixtures/team_repo.yml b/models/fixtures/team_repo.yml
new file mode 100644
index 0000000000..b954ae8c10
--- /dev/null
+++ b/models/fixtures/team_repo.yml
@@ -0,0 +1,5 @@
+-
+ id: 1
+ org_id: 3
+ team_id: 1
+ repo_id: 3
diff --git a/models/fixtures/team_user.yml b/models/fixtures/team_user.yml
new file mode 100644
index 0000000000..e569f9f3d9
--- /dev/null
+++ b/models/fixtures/team_user.yml
@@ -0,0 +1,5 @@
+-
+ id: 1
+ org_id: 3
+ team_id: 1
+ uid: 2
diff --git a/models/fixtures/user.yml b/models/fixtures/user.yml
index 8847fc7aaa..cb3b954a22 100644
--- a/models/fixtures/user.yml
+++ b/models/fixtures/user.yml
@@ -23,3 +23,42 @@
avatar: avatar2
avatar_email: user2@example.com
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
diff --git a/models/setup_for_test.go b/models/setup_for_test.go
index b585a7490e..cf88397c26 100644
--- a/models/setup_for_test.go
+++ b/models/setup_for_test.go
@@ -12,6 +12,7 @@ import (
"github.com/go-xorm/core"
"github.com/go-xorm/xorm"
_ "github.com/mattn/go-sqlite3" // for the test engine
+ "github.com/stretchr/testify/assert"
"gopkg.in/testfixtures.v2"
)
@@ -45,3 +46,17 @@ func CreateTestEngine() error {
func PrepareTestDatabase() error {
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)
+}