From 62a1322cf9940daa987ca16053f5081d7da54298 Mon Sep 17 00:00:00 2001 From: guillep2k <18600385+guillep2k@users.noreply.github.com> Date: Sat, 15 Feb 2020 01:29:06 -0300 Subject: [PATCH] Fix "access" fixtures and tests (#10247) * Add fixture gen tool and fix "access" test * Close file before exiting * Add missing repo_unit for repo id: 5 * Fix count on TestAPIOrgRepos Co-authored-by: Lunny Xiao --- integrations/api_repo_test.go | 2 +- models/access_test.go | 2 +- models/fixture_access_test.go | 52 +++++++++++++ models/fixtures/access.yml | 121 +++++++++++++++++++----------- models/fixtures/collaboration.yml | 24 ++++++ models/fixtures/repo_unit.yml | 37 ++++++++- 6 files changed, 193 insertions(+), 45 deletions(-) create mode 100644 models/fixture_access_test.go diff --git a/integrations/api_repo_test.go b/integrations/api_repo_test.go index 715fc629a..ff59fac5c 100644 --- a/integrations/api_repo_test.go +++ b/integrations/api_repo_test.go @@ -231,7 +231,7 @@ func TestAPIOrgRepos(t *testing.T) { includesPrivate bool }{ nil: {count: 1}, - user: {count: 2, includesPrivate: true}, + user: {count: 3, includesPrivate: true}, user2: {count: 3, includesPrivate: true}, user3: {count: 1}, } diff --git a/models/access_test.go b/models/access_test.go index 103fe3a68..c134ab98d 100644 --- a/models/access_test.go +++ b/models/access_test.go @@ -112,7 +112,7 @@ func TestUser_GetAccessibleRepositories(t *testing.T) { user2 := AssertExistsAndLoadBean(t, &User{ID: 2}).(*User) repos, err = user2.GetAccessibleRepositories(0) assert.NoError(t, err) - assert.Len(t, repos, 1) + assert.Len(t, repos, 4) user29 := AssertExistsAndLoadBean(t, &User{ID: 29}).(*User) repos, err = user29.GetAccessibleRepositories(0) diff --git a/models/fixture_access_test.go b/models/fixture_access_test.go new file mode 100644 index 000000000..9a50d24d7 --- /dev/null +++ b/models/fixture_access_test.go @@ -0,0 +1,52 @@ +// Copyright 2020 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. + +// +build access_fixtures + +package models + +// This file is excluded from build and tests, and is intended for assisting +// in keeping access.yml in sync with the other .yml files. + +// To use it, do: +// cd models +// go test -tags "access_fixtures sqlite sqlite_unlock_notify" -run TestBuildAccessFixturesYaml + +import ( + "bufio" + "fmt" + "os" + "testing" + + "github.com/stretchr/testify/assert" +) + +func TestBuildAccessFixturesYaml(t *testing.T) { + assert.NoError(t, PrepareTestDatabase()) + + repos := make([]*Repository, 0, 50) + assert.NoError(t, x.Find(&repos)) + for _, repo := range repos { + repo.MustOwner() + assert.NoError(t, repo.RecalculateAccesses()) + } + + f, err := os.Create("fixtures/access.yml") + assert.NoError(t, err) + w := bufio.NewWriter(f) + + accesses := make([]*Access, 0, 200) + assert.NoError(t, x.OrderBy("user_id, repo_id").Find(&accesses)) + for i, a := range accesses { + fmt.Fprintf(w, "-\n") + fmt.Fprintf(w, " id: %d\n", i+1) + fmt.Fprintf(w, " user_id: %d\n", a.UserID) + fmt.Fprintf(w, " repo_id: %d\n", a.RepoID) + fmt.Fprintf(w, " mode: %d\n", a.Mode) + fmt.Fprintf(w, "\n") + } + + w.Flush() + f.Close() +} diff --git a/models/fixtures/access.yml b/models/fixtures/access.yml index 811720c8e..4027e5fe9 100644 --- a/models/fixtures/access.yml +++ b/models/fixtures/access.yml @@ -2,88 +2,125 @@ id: 1 user_id: 2 repo_id: 3 - mode: 2 # write + mode: 4 - id: 2 - user_id: 4 - repo_id: 4 - mode: 2 # write + user_id: 2 + repo_id: 5 + mode: 4 - id: 3 - user_id: 4 - repo_id: 3 - mode: 2 # write + user_id: 2 + repo_id: 24 + mode: 2 - id: 4 - user_id: 15 - repo_id: 22 - mode: 2 # write + user_id: 2 + repo_id: 32 + mode: 4 - id: 5 - user_id: 15 - repo_id: 21 - mode: 2 # write + user_id: 4 + repo_id: 3 + mode: 2 - id: 6 - user_id: 15 - repo_id: 23 - mode: 4 # owner + user_id: 4 + repo_id: 4 + mode: 2 - id: 7 - user_id: 15 - repo_id: 24 - mode: 4 # owner + user_id: 4 + repo_id: 40 + mode: 2 - id: 8 - user_id: 18 - repo_id: 23 - mode: 4 # owner + user_id: 15 + repo_id: 21 + mode: 2 - id: 9 - user_id: 18 - repo_id: 24 - mode: 4 # owner + user_id: 15 + repo_id: 22 + mode: 2 - id: 10 - user_id: 18 - repo_id: 22 - mode: 2 # write + user_id: 15 + repo_id: 23 + mode: 4 - id: 11 - user_id: 18 - repo_id: 21 - mode: 2 # write + user_id: 15 + repo_id: 24 + mode: 4 - id: 12 - user_id: 20 - repo_id: 27 - mode: 4 # owner - + user_id: 15 + repo_id: 32 + mode: 2 + - id: 13 - user_id: 20 - repo_id: 28 - mode: 4 # owner + user_id: 18 + repo_id: 21 + mode: 2 - id: 14 - user_id: 29 - repo_id: 4 - mode: 2 # write (collaborator) + user_id: 18 + repo_id: 22 + mode: 2 - id: 15 + user_id: 18 + repo_id: 23 + mode: 4 + +- + id: 16 + user_id: 18 + repo_id: 24 + mode: 4 + +- + id: 17 + user_id: 20 + repo_id: 24 + mode: 1 + +- + id: 18 + user_id: 20 + repo_id: 27 + mode: 4 + +- + id: 19 + user_id: 20 + repo_id: 28 + mode: 4 + +- + id: 20 + user_id: 29 + repo_id: 4 + mode: 2 + +- + id: 21 user_id: 29 repo_id: 24 - mode: 1 # read + mode: 1 + diff --git a/models/fixtures/collaboration.yml b/models/fixtures/collaboration.yml index 82d46f38f..ef77d22b2 100644 --- a/models/fixtures/collaboration.yml +++ b/models/fixtures/collaboration.yml @@ -21,3 +21,27 @@ repo_id: 4 user_id: 29 mode: 2 # write + +- + id: 5 + repo_id: 21 + user_id: 15 + mode: 2 # write + +- + id: 6 + repo_id: 21 + user_id: 18 + mode: 2 # write + +- + id: 7 + repo_id: 22 + user_id: 15 + mode: 2 # write + +- + id: 8 + repo_id: 22 + user_id: 18 + mode: 2 # write diff --git a/models/fixtures/repo_unit.yml b/models/fixtures/repo_unit.yml index 5ced38b00..35b9b92b7 100644 --- a/models/fixtures/repo_unit.yml +++ b/models/fixtures/repo_unit.yml @@ -478,4 +478,39 @@ repo_id: 2 type: 2 config: "{}" - created_unix: 946684810 \ No newline at end of file + created_unix: 946684810 + +- + id: 70 + repo_id: 5 + type: 4 + config: "{}" + created_unix: 946684810 + +- + id: 71 + repo_id: 5 + type: 5 + config: "{}" + created_unix: 946684810 + +- + id: 72 + repo_id: 5 + type: 1 + config: "{}" + created_unix: 946684810 + +- + id: 73 + repo_id: 5 + type: 2 + config: "{\"EnableTimetracker\":true,\"AllowOnlyContributorsToTrackTime\":true}" + created_unix: 946684810 + +- + id: 74 + repo_id: 5 + type: 3 + config: "{\"IgnoreWhitespaceConflicts\":false,\"AllowMerge\":true,\"AllowRebase\":true,\"AllowRebaseMerge\":true,\"AllowSquash\":true}" + created_unix: 946684810