|
4 | 4 | require 'models/repository' |
5 | 5 | require 'models/build' |
6 | 6 | require 'models/job' |
| 7 | +require 'models/organization' |
| 8 | +require 'models/user' |
7 | 9 | require 'support/factories' |
8 | 10 | require 'pry' |
9 | 11 |
|
10 | 12 | describe Backup do |
11 | 13 | before(:all) do |
12 | | - system("psql '#{Config.new.database_url}' -f db/schema.sql") |
| 14 | + system("psql '#{Config.new.database_url}' -f db/schema.sql > /dev/null") |
13 | 15 | end |
14 | 16 |
|
15 | | - after(:each) do |
16 | | - Repository.destroy_all |
17 | | - Build.destroy_all |
18 | | - Job.destroy_all |
19 | | - end |
20 | | - |
21 | | - let!(:config) { Config.new } |
22 | 17 | let(:files_location) { "dump/tests" } |
23 | 18 | let!(:backup) { Backup.new(files_location: files_location, limit: 2) } |
24 | | - let(:datetime) { (config.delay + 1).months.ago.to_time.utc } |
25 | | - let(:org_id) { rand(100000) } |
26 | | - let(:com_id) { rand(100000) } |
27 | | - let(:private_org_id) { rand(100000) } |
28 | | - let(:private_com_id) { rand(100000) } |
29 | | - let!(:repository) { |
30 | | - FactoryBot.create( |
31 | | - :repository_with_builds, |
32 | | - created_at: datetime, |
33 | | - updated_at: datetime |
34 | | - ) |
35 | | - } |
| 19 | + |
| 20 | + describe 'export' do |
| 21 | + let!(:unassigned_repositories) { |
| 22 | + (1..3).to_a.map do |
| 23 | + FactoryBot.create(:repository) |
| 24 | + end |
| 25 | + } |
| 26 | + let!(:user1) { |
| 27 | + FactoryBot.create(:user_with_repos) |
| 28 | + } |
| 29 | + let!(:user2) { |
| 30 | + FactoryBot.create(:user_with_repos) |
| 31 | + } |
| 32 | + let!(:organization1) { |
| 33 | + FactoryBot.create(:organization_with_repos) |
| 34 | + } |
| 35 | + let!(:organization2) { |
| 36 | + FactoryBot.create(:organization_with_repos) |
| 37 | + } |
| 38 | + |
| 39 | + context 'when no arguments are given' do |
| 40 | + it 'processes every repository' do |
| 41 | + Repository.all.each do |repository| |
| 42 | + expect(backup).to receive(:process_repo).once.with(repository) |
| 43 | + end |
| 44 | + backup.export |
| 45 | + end |
| 46 | + end |
| 47 | + context 'when user_id is given' do |
| 48 | + it 'processes only the repositories of the given user' do |
| 49 | + processed_repos_ids = [] |
| 50 | + allow(backup).to receive(:process_repo) {|repo| processed_repos_ids.push(repo.id)} |
| 51 | + user_repos_ids = Repository.where( |
| 52 | + 'owner_id = ? and owner_type = ?', |
| 53 | + user1.id, |
| 54 | + 'User' |
| 55 | + ).map(&:id) |
| 56 | + backup.export(user_id: user1.id) |
| 57 | + expect(processed_repos_ids).to match_array(user_repos_ids) |
| 58 | + end |
| 59 | + end |
| 60 | + context 'when org_id is given' do |
| 61 | + it 'processes only the repositories of the given organization' do |
| 62 | + processed_repos_ids = [] |
| 63 | + allow(backup).to receive(:process_repo) {|repo| processed_repos_ids.push(repo.id)} |
| 64 | + organization_repos_ids = Repository.where( |
| 65 | + 'owner_id = ? and owner_type = ?', |
| 66 | + organization1.id, |
| 67 | + 'Organization' |
| 68 | + ).map(&:id) |
| 69 | + backup.export(org_id: organization1.id) |
| 70 | + expect(processed_repos_ids).to match_array(organization_repos_ids) |
| 71 | + end |
| 72 | + end |
| 73 | + context 'when repo_id is given' do |
| 74 | + it 'processes only the repository with the given id' do |
| 75 | + repo = Repository.first |
| 76 | + expect(backup).to receive(:process_repo).once.with(repo) |
| 77 | + backup.export(repo_id: repo.id) |
| 78 | + end |
| 79 | + end |
| 80 | + end |
36 | 81 |
|
37 | 82 | describe 'process_repo' do |
| 83 | + after(:each) do |
| 84 | + Repository.destroy_all |
| 85 | + Build.destroy_all |
| 86 | + Job.destroy_all |
| 87 | + end |
| 88 | + |
| 89 | + let!(:config) { Config.new } |
| 90 | + let(:datetime) { (config.delay + 1).months.ago.to_time.utc } |
| 91 | + let(:org_id) { rand(100000) } |
| 92 | + let(:com_id) { rand(100000) } |
| 93 | + let(:private_org_id) { rand(100000) } |
| 94 | + let(:private_com_id) { rand(100000) } |
| 95 | + let!(:repository) { |
| 96 | + FactoryBot.create( |
| 97 | + :repository_with_builds, |
| 98 | + created_at: datetime, |
| 99 | + updated_at: datetime |
| 100 | + ) |
| 101 | + } |
| 102 | + |
38 | 103 | let!(:exported_object) { |
39 | 104 | [[ |
40 | 105 | { |
|
0 commit comments