From 6072b032918006824f260e79f96372dd94fbb17f Mon Sep 17 00:00:00 2001 From: Ethan Koenig Date: Sat, 7 Jan 2017 22:10:53 -0500 Subject: [PATCH] Unit tests for models/access.go (#606) --- models/access_test.go | 136 ++++++++++++++++++++++++++++++ models/fixtures/access.yml | 11 +++ models/fixtures/collaboration.yml | 11 +++ models/fixtures/org_user.yml | 7 ++ models/fixtures/repository.yml | 22 +++++ models/fixtures/team.yml | 7 ++ models/fixtures/team_repo.yml | 5 ++ models/fixtures/team_user.yml | 5 ++ models/fixtures/user.yml | 39 +++++++++ models/setup_for_test.go | 15 ++++ 10 files changed, 258 insertions(+) create mode 100644 models/access_test.go create mode 100644 models/fixtures/access.yml create mode 100644 models/fixtures/collaboration.yml create mode 100644 models/fixtures/org_user.yml create mode 100644 models/fixtures/team.yml create mode 100644 models/fixtures/team_repo.yml create mode 100644 models/fixtures/team_user.yml 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) +}