123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289 |
- // Copyright 2020 The Gogs 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 db
- import (
- "testing"
- "github.com/stretchr/testify/assert"
- )
- func Test_perms(t *testing.T) {
- if testing.Short() {
- t.Skip()
- }
- t.Parallel()
- tables := []interface{}{new(Access)}
- db := &perms{
- DB: initTestDB(t, "perms", tables...),
- }
- for _, tc := range []struct {
- name string
- test func(*testing.T, *perms)
- }{
- {"AccessMode", test_perms_AccessMode},
- {"Authorize", test_perms_Authorize},
- {"SetRepoPerms", test_perms_SetRepoPerms},
- } {
- t.Run(tc.name, func(t *testing.T) {
- t.Cleanup(func() {
- err := clearTables(db.DB, tables...)
- if err != nil {
- t.Fatal(err)
- }
- })
- tc.test(t, db)
- })
- }
- }
- func test_perms_AccessMode(t *testing.T, db *perms) {
- // Set up permissions
- err := db.SetRepoPerms(1, map[int64]AccessMode{
- 2: AccessModeWrite,
- 3: AccessModeAdmin,
- })
- if err != nil {
- t.Fatal(err)
- }
- err = db.SetRepoPerms(2, map[int64]AccessMode{
- 1: AccessModeRead,
- })
- if err != nil {
- t.Fatal(err)
- }
- publicRepo := &Repository{
- ID: 1,
- OwnerID: 98,
- }
- privateRepo := &Repository{
- ID: 2,
- OwnerID: 99,
- IsPrivate: true,
- }
- tests := []struct {
- name string
- userID int64
- repo *Repository
- expAccessMode AccessMode
- }{
- {
- name: "nil repository",
- expAccessMode: AccessModeNone,
- },
- {
- name: "anonymous user has read access to public repository",
- repo: publicRepo,
- expAccessMode: AccessModeRead,
- },
- {
- name: "anonymous user has no access to private repository",
- repo: privateRepo,
- expAccessMode: AccessModeNone,
- },
- {
- name: "user is the owner",
- userID: 98,
- repo: publicRepo,
- expAccessMode: AccessModeOwner,
- },
- {
- name: "user 1 has read access to public repo",
- userID: 1,
- repo: publicRepo,
- expAccessMode: AccessModeRead,
- },
- {
- name: "user 2 has write access to public repo",
- userID: 2,
- repo: publicRepo,
- expAccessMode: AccessModeWrite,
- },
- {
- name: "user 3 has admin access to public repo",
- userID: 3,
- repo: publicRepo,
- expAccessMode: AccessModeAdmin,
- },
- {
- name: "user 1 has read access to private repo",
- userID: 1,
- repo: privateRepo,
- expAccessMode: AccessModeRead,
- },
- {
- name: "user 2 has no access to private repo",
- userID: 2,
- repo: privateRepo,
- expAccessMode: AccessModeNone,
- },
- {
- name: "user 3 has no access to private repo",
- userID: 3,
- repo: privateRepo,
- expAccessMode: AccessModeNone,
- },
- }
- for _, test := range tests {
- t.Run(test.name, func(t *testing.T) {
- mode := db.AccessMode(test.userID, test.repo)
- assert.Equal(t, test.expAccessMode, mode)
- })
- }
- }
- func test_perms_Authorize(t *testing.T, db *perms) {
- // Set up permissions
- err := db.SetRepoPerms(1, map[int64]AccessMode{
- 1: AccessModeRead,
- 2: AccessModeWrite,
- 3: AccessModeAdmin,
- })
- if err != nil {
- t.Fatal(err)
- }
- repo := &Repository{
- ID: 1,
- OwnerID: 98,
- }
- tests := []struct {
- name string
- userID int64
- desired AccessMode
- expAuthorized bool
- }{
- {
- name: "user 1 has read and wants read",
- userID: 1,
- desired: AccessModeRead,
- expAuthorized: true,
- },
- {
- name: "user 1 has read and wants write",
- userID: 1,
- desired: AccessModeWrite,
- expAuthorized: false,
- },
- {
- name: "user 2 has write and wants read",
- userID: 2,
- desired: AccessModeRead,
- expAuthorized: true,
- },
- {
- name: "user 2 has write and wants write",
- userID: 2,
- desired: AccessModeWrite,
- expAuthorized: true,
- },
- {
- name: "user 2 has write and wants admin",
- userID: 2,
- desired: AccessModeAdmin,
- expAuthorized: false,
- },
- {
- name: "user 3 has admin and wants read",
- userID: 3,
- desired: AccessModeRead,
- expAuthorized: true,
- },
- {
- name: "user 3 has admin and wants write",
- userID: 3,
- desired: AccessModeWrite,
- expAuthorized: true,
- },
- {
- name: "user 3 has admin and wants admin",
- userID: 3,
- desired: AccessModeAdmin,
- expAuthorized: true,
- },
- }
- for _, test := range tests {
- t.Run(test.name, func(t *testing.T) {
- authorized := db.Authorize(test.userID, repo, test.desired)
- assert.Equal(t, test.expAuthorized, authorized)
- })
- }
- }
- func test_perms_SetRepoPerms(t *testing.T, db *perms) {
- for _, update := range []struct {
- repoID int64
- accessMap map[int64]AccessMode
- }{
- {
- repoID: 1,
- accessMap: map[int64]AccessMode{
- 1: AccessModeWrite,
- 2: AccessModeWrite,
- 3: AccessModeAdmin,
- 4: AccessModeWrite,
- },
- },
- {
- repoID: 2,
- accessMap: map[int64]AccessMode{
- 1: AccessModeWrite,
- 2: AccessModeRead,
- 4: AccessModeWrite,
- 5: AccessModeWrite,
- },
- },
- {
- repoID: 1,
- accessMap: map[int64]AccessMode{
- 2: AccessModeWrite,
- 3: AccessModeAdmin,
- },
- },
- {
- repoID: 2,
- accessMap: map[int64]AccessMode{
- 1: AccessModeWrite,
- 2: AccessModeRead,
- 5: AccessModeWrite,
- },
- },
- } {
- err := db.SetRepoPerms(update.repoID, update.accessMap)
- if err != nil {
- t.Fatal(err)
- }
- }
- var accesses []*Access
- err := db.Order("user_id, repo_id").Find(&accesses).Error
- if err != nil {
- t.Fatal(err)
- }
- // Ignore ID fields
- for _, a := range accesses {
- a.ID = 0
- }
- expAccesses := []*Access{
- {UserID: 1, RepoID: 2, Mode: AccessModeWrite},
- {UserID: 2, RepoID: 1, Mode: AccessModeWrite},
- {UserID: 2, RepoID: 2, Mode: AccessModeRead},
- {UserID: 3, RepoID: 1, Mode: AccessModeAdmin},
- {UserID: 5, RepoID: 2, Mode: AccessModeWrite},
- }
- assert.Equal(t, expAccesses, accesses)
- }
|