Commit 2662fd72 authored by Grzegorz Bizon's avatar Grzegorz Bizon

Merge branch '33139-lint-our-factories' into 'master'

Lint our factories creation in addition to their build

Closes #33139

See merge request !11823
parents 11852e16 857d0391
...@@ -14,7 +14,7 @@ class Commit ...@@ -14,7 +14,7 @@ class Commit
participant :committer participant :committer
participant :notes_with_associations participant :notes_with_associations
attr_accessor :project attr_accessor :project, :author
DIFF_SAFE_LINES = Gitlab::Git::DiffCollection::DEFAULT_LIMITS[:max_lines] DIFF_SAFE_LINES = Gitlab::Git::DiffCollection::DEFAULT_LIMITS[:max_lines]
......
...@@ -49,7 +49,7 @@ describe Profiles::KeysController do ...@@ -49,7 +49,7 @@ describe Profiles::KeysController do
expect(response.body).to eq(user.all_ssh_keys.join("\n")) expect(response.body).to eq(user.all_ssh_keys.join("\n"))
expect(response.body).to include(key.key.sub(' dummy@gitlab.com', '')) expect(response.body).to include(key.key.sub(' dummy@gitlab.com', ''))
expect(response.body).to include(another_key.key) expect(response.body).to include(another_key.key.sub(' dummy@gitlab.com', ''))
expect(response.body).not_to include(deploy_key.key) expect(response.body).not_to include(deploy_key.key)
end end
......
...@@ -9,14 +9,14 @@ FactoryGirl.define do ...@@ -9,14 +9,14 @@ FactoryGirl.define do
factory :ci_pipeline_without_jobs do factory :ci_pipeline_without_jobs do
after(:build) do |pipeline| after(:build) do |pipeline|
allow(pipeline).to receive(:ci_yaml_file) { YAML.dump({}) } pipeline.instance_variable_set(:@ci_yaml_file, YAML.dump({}))
end end
end end
factory :ci_pipeline_with_one_job do factory :ci_pipeline_with_one_job do
after(:build) do |pipeline| after(:build) do |pipeline|
allow(pipeline).to receive(:ci_yaml_file) do allow(pipeline).to receive(:ci_yaml_file) do
YAML.dump({ rspec: { script: "ls" } }) pipeline.instance_variable_set(:@ci_yaml_file, YAML.dump({ rspec: { script: "ls" } }))
end end
end end
end end
...@@ -34,17 +34,14 @@ FactoryGirl.define do ...@@ -34,17 +34,14 @@ FactoryGirl.define do
transient { config nil } transient { config nil }
after(:build) do |pipeline, evaluator| after(:build) do |pipeline, evaluator|
allow(pipeline).to receive(:ci_yaml_file) do if evaluator.config
if evaluator.config pipeline.instance_variable_set(:@ci_yaml_file, YAML.dump(evaluator.config))
YAML.dump(evaluator.config)
else
File.read(Rails.root.join('spec/support/gitlab_stubs/gitlab_ci.yml'))
end
end
# Populates pipeline with errors # Populates pipeline with errors
# pipeline.config_processor if evaluator.config
pipeline.config_processor if evaluator.config else
pipeline.instance_variable_set(:@ci_yaml_file, File.read(Rails.root.join('spec/support/gitlab_stubs/gitlab_ci.yml')))
end
end end
trait :invalid do trait :invalid do
......
FactoryGirl.define do FactoryGirl.define do
factory :ci_stage, class: Ci::Stage do factory :ci_stage, class: Ci::Stage do
skip_create
transient do transient do
name 'test' name 'test'
status nil status nil
......
FactoryGirl.define do FactoryGirl.define do
factory :ci_trigger_request, class: Ci::TriggerRequest do factory :ci_trigger_request, class: Ci::TriggerRequest do
factory :ci_trigger_request_with_variables do trigger factory: :ci_trigger
trigger factory: :ci_trigger
factory :ci_trigger_request_with_variables do
variables do variables do
{ {
TRIGGER_KEY_1: 'TRIGGER_VALUE_1', TRIGGER_KEY_1: 'TRIGGER_VALUE_1',
......
...@@ -4,19 +4,14 @@ FactoryGirl.define do ...@@ -4,19 +4,14 @@ FactoryGirl.define do
factory :commit do factory :commit do
git_commit RepoHelpers.sample_commit git_commit RepoHelpers.sample_commit
project factory: :empty_project project factory: :empty_project
author { build(:author) }
initialize_with do initialize_with do
new(git_commit, project) new(git_commit, project)
end end
after(:build) do |commit|
allow(commit).to receive(:author).and_return build(:author)
end
trait :without_author do trait :without_author do
after(:build) do |commit| author nil
allow(commit).to receive(:author).and_return nil
end
end end
end end
end end
FactoryGirl.define do FactoryGirl.define do
factory :file_uploader do factory :file_uploader do
skip_create
project factory: :empty_project project factory: :empty_project
secret nil secret nil
......
require_relative '../support/helpers/key_generator_helper'
FactoryGirl.define do FactoryGirl.define do
factory :key do factory :key do
title title
key do key { Spec::Support::Helpers::KeyGeneratorHelper.new(1024).generate + ' dummy@gitlab.com' }
'ssh-rsa AAAAB3NzaC1yc2EAAAABJQAAAIEAiPWx6WM4lhHNedGfBpPJNPpZ7yKu+dnn1SJejgt4596k6YjzGGphH2TUxwKzxcKDKKezwkpfnxPkSMkuEspGRt/aZZ9wa++Oi7Qkr8prgHc4soW6NUlfDzpvZK2H5E7eQaSeP3SAwGmQKUFHCddNaP0L+hM7zhFNzjFvpaMgJw0= dummy@gitlab.com'
end
factory :deploy_key, class: 'DeployKey' do factory :deploy_key, class: 'DeployKey'
key do
'ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDFf6RYK3qu/RKF/3ndJmL5xgMLp3O96x8lTay+QGZ0+9FnnAXMdUqBq/ZU6d/gyMB4IaW3nHzM1w049++yAB6UPCzMB8Uo27K5/jyZCtj7Vm9PFNjF/8am1kp46c/SeYicQgQaSBdzIW3UDEa1Ef68qroOlvpi9PYZ/tA7M0YP0K5PXX+E36zaIRnJVMPT3f2k+GnrxtjafZrwFdpOP/Fol5BQLBgcsyiU+LM1SuaCrzd8c9vyaTA1CxrkxaZh+buAi0PmdDtaDrHd42gqZkXCKavyvgM5o2CkQ5LJHCgzpXy05qNFzmThBSkb+XtoxbyagBiGbVZtSVow6Xa7qewz'
end
end
factory :personal_key do factory :personal_key do
user user
end end
factory :another_key do factory :another_key do
key do factory :another_deploy_key, class: 'DeployKey'
'ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDmTillFzNTrrGgwaCKaSj+QCz81E6jBc/s9av0+3b1Hwfxgkqjl4nAK/OD2NjgyrONDTDfR8cRN4eAAy6nY8GLkOyYBDyuc5nTMqs5z3yVuTwf3koGm/YQQCmo91psZ2BgDFTor8SVEE5Mm1D1k3JDMhDFxzzrOtRYFPci9lskTJaBjpqWZ4E9rDTD2q/QZntCqbC3wE9uSemRQB5f8kik7vD/AD8VQXuzKladrZKkzkONCPWsXDspUitjM8HkQdOf0PsYn1CMUC1xKYbCxkg5TkEosIwGv6CoEArUrdu/4+10LVslq494mAvEItywzrluCLCnwELfW+h/m8UHoVhZ'
end
factory :another_deploy_key, class: 'DeployKey' do
end
end end
factory :write_access_key, class: 'DeployKey' do factory :write_access_key, class: 'DeployKey' do
......
FactoryGirl.define do FactoryGirl.define do
factory :project_statistics do factory :project_statistics do
project { create :project } project
namespace { project.namespace }
initialize_with do
# statistics are automatically created when a project is created
project&.statistics || new
end
end end
end end
FactoryGirl.define do FactoryGirl.define do
factory :project_wiki do factory :project_wiki do
skip_create
project factory: :empty_project project factory: :empty_project
user factory: :user user factory: :user
initialize_with { new(project, user) } initialize_with { new(project, user) }
......
require_relative '../support/test_env'
FactoryGirl.define do FactoryGirl.define do
# Project without repository # Project without repository
# #
......
FactoryGirl.define do FactoryGirl.define do
factory :wiki_directory do factory :wiki_directory do
skip_create
slug '/path_up_to/dir' slug '/path_up_to/dir'
initialize_with { new(slug) } initialize_with { new(slug) }
end end
......
...@@ -3,14 +3,20 @@ require 'spec_helper' ...@@ -3,14 +3,20 @@ require 'spec_helper'
describe 'factories' do describe 'factories' do
FactoryGirl.factories.each do |factory| FactoryGirl.factories.each do |factory|
describe "#{factory.name} factory" do describe "#{factory.name} factory" do
let(:entity) { build(factory.name) } it 'does not raise error when built' do
expect { build(factory.name) }.not_to raise_error
end
it 'does not raise error when created' do it 'does not raise error when created' do
expect { entity }.not_to raise_error expect { create(factory.name) }.not_to raise_error
end end
it 'is valid', if: factory.build_class < ActiveRecord::Base do factory.definition.defined_traits.map(&:name).each do |trait_name|
expect(entity).to be_valid describe "linting #{trait_name} trait" do
skip 'does not raise error when created' do
expect { create(factory.name, trait_name) }.not_to raise_error
end
end
end end
end end
end end
......
...@@ -66,14 +66,16 @@ describe Key, models: true do ...@@ -66,14 +66,16 @@ describe Key, models: true do
end end
it "does not accept the exact same key twice" do it "does not accept the exact same key twice" do
create(:key, user: user) first_key = create(:key, user: user)
expect(build(:key, user: user)).not_to be_valid
expect(build(:key, user: user, key: first_key.key)).not_to be_valid
end end
it "does not accept a duplicate key with a different comment" do it "does not accept a duplicate key with a different comment" do
create(:key, user: user) first_key = create(:key, user: user)
duplicate = build(:key, user: user) duplicate = build(:key, user: user, key: first_key.key)
duplicate.key << ' extra comment' duplicate.key << ' extra comment'
expect(duplicate).not_to be_valid expect(duplicate).not_to be_valid
end end
end end
......
module Spec
module Support
module Helpers
class KeyGeneratorHelper
# The components in a openssh .pub / known_host RSA public key.
RSA_COMPONENTS = ['ssh-rsa', :e, :n].freeze
attr_reader :size
def initialize(size = 2048)
@size = size
end
def generate
key = OpenSSL::PKey::RSA.generate(size)
components = RSA_COMPONENTS.map do |component|
key.respond_to?(component) ? encode_mpi(key.public_send(component)) : component
end
# Ruby tries to be helpful and adds new lines every 60 bytes :(
'ssh-rsa ' + [pack_pubkey_components(components)].pack('m').delete("\n")
end
private
# Encodes an openssh-mpi-encoded integer.
def encode_mpi(n)
chars, n = [], n.to_i
chars << (n & 0xff) && n >>= 8 while n != 0
chars << 0 if chars.empty? || chars.last >= 0x80
chars.reverse.pack('C*')
end
# Packs string components into an openssh-encoded pubkey.
def pack_pubkey_components(strings)
(strings.map { |s| [s.length].pack('N') }).zip(strings).flatten.join
end
end
end
end
end
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment