summaryrefslogtreecommitdiff
path: root/spec/support/helpers/packages_manager_api_spec_helper.rb
blob: e5a690e1680c47cbdd208eebbd083b4bf527221c (plain)
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
# frozen_string_literal: true

module PackagesManagerApiSpecHelpers
  def build_auth_headers(value)
    { 'HTTP_AUTHORIZATION' => value }
  end

  def build_basic_auth_header(username, password)
    build_auth_headers(ActionController::HttpAuthentication::Basic.encode_credentials(username, password))
  end

  def build_token_auth_header(token)
    build_auth_headers("Bearer #{token}")
  end

  def build_jwt(personal_access_token, secret: jwt_secret, user_id: nil)
    JSONWebToken::HMACToken.new(secret).tap do |jwt|
      jwt['access_token'] = personal_access_token.id
      jwt['user_id'] = user_id || personal_access_token.user_id
    end
  end

  def build_jwt_from_job(job, secret: jwt_secret)
    JSONWebToken::HMACToken.new(secret).tap do |jwt|
      jwt['access_token'] = job.token
      jwt['user_id'] = job.user.id
    end
  end

  def build_jwt_from_deploy_token(deploy_token, secret: jwt_secret)
    JSONWebToken::HMACToken.new(secret).tap do |jwt|
      jwt['access_token'] = deploy_token.token
      jwt['user_id'] = deploy_token.username
    end
  end

  def temp_file(package_tmp)
    upload_path = ::Packages::PackageFileUploader.workhorse_local_upload_path
    file_path = "#{upload_path}/#{package_tmp}"

    FileUtils.mkdir_p(upload_path)
    File.write(file_path, 'test')

    UploadedFile.new(file_path, filename: File.basename(file_path))
  end
end