File: spec_helper.rb

package info (click to toggle)
debci 3.13
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,656 kB
  • sloc: ruby: 6,516; sh: 2,437; javascript: 100; makefile: 92; perl: 11
file content (109 lines) | stat: -rw-r--r-- 2,806 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
if ENV["COVERAGE"] != "no"
  begin
    require 'simplecov'
    RSpec.configure do |config|
      unless config.files_to_run.one?
        SimpleCov.start do
          minimum_coverage(93)
          track_files('lib/**/*.rb')
          add_filter(/migrations/)
          add_filter(/spec/)
        end
      end
    end
  rescue LoadError
    $stderr.puts "W: simplecov not installed, not checking test coverage"
  end
end

ENV['DATABASE_URL'] ||= 'sqlite3::memory:'
ENV['RACK_ENV'] = 'test'

require 'fileutils'
require 'rack/test'
require 'tmpdir'
require 'yaml'
require 'debci/db'
require 'debci/job'
require 'debci/app'

if Gem::Specification.find_by_name("bullet").version >= Gem::Version.new("7")
  require 'bullet'
  module Debci
    class App
      configure do
        Bullet.enable = true
        Bullet.bullet_logger = true
        Bullet.raise = true
        use Bullet::Rack
      end
    end
  end
end

require 'database_cleaner'
require 'omniauth'
DatabaseCleaner.allow_remote_database_url = true
DatabaseCleaner.strategy = :transaction

Debci.config.backend = 'fake'
Debci.config.quiet = true
Debci::DB.migrate

arch = `dpkg --print-architecture`.strip

RSpec.shared_context 'tmpdir' do
  let(:arch) { arch }
  let(:tmpdir) { Dir.mktmpdir("debci-tests-") }
  after(:each) { FileUtils.rm_rf(tmpdir) }
end

RSpec.shared_context 'no-rabbitmq' do
  before(:each) do
    allow_any_instance_of(Debci::Job).to receive(:enqueue)
  end
end

RSpec.configure do |config|
  config.include_context 'tmpdir'
  config.before(:each) do
    allow(Debci).to receive(:warn)
    allow_any_instance_of(Debci::Config).to receive(:arch_list).and_return([arch])
    allow_any_instance_of(Debci::Config).to receive(:suite_list).and_return(['unstable', 'testing'])
    allow_any_instance_of(Debci::Config).to receive(:data_basedir).and_return(File.join(tmpdir, '/data'))
    allow_any_instance_of(Debci::Config).to receive(:html_dir).and_return(File.join(tmpdir, '/data/.html'))
  end
  config.before(:each) do
    DatabaseCleaner.start
  end
  config.after(:each) do
    DatabaseCleaner.clean
  end
end

OmniAuth.config.test_mode = true

# module for logging in in tests. Note that for using this, the test scenario
# needs to have the self service app mounted at /user, besides the app being
# tested.
module WebAppHelper
  include Rack::Test::Methods

  def fake_login(username)
    env("rack.session", { :user_id => Debci::User.create!(username: username).id })
  end

  def login(username, uid = nil)
    uid ||= username.hash % 1000
    OmniAuth.config.mock_auth[:gitlab] = OmniAuth::AuthHash.new({
      provider: 'gitlab',
      uid: uid,
      info: OmniAuth::AuthHash::InfoHash.new({ username: username })
    })
    get '/user/auth/gitlab/callback'
  end

  def logout
    env("rack.session", {})
  end
end