Initial commit

This commit is contained in:
Senad Uka
2020-05-31 22:38:19 +02:00
commit 858fafc3c5
1280 changed files with 65918 additions and 0 deletions

View File

@@ -0,0 +1,3 @@
RSpec.configure do |config|
config.include ActionMailer::TestHelper
end

View File

@@ -0,0 +1 @@
ActiveJob::Base.queue_adapter = :test

View File

@@ -0,0 +1,62 @@
module ApiResponseHelper
class ApiResponseServiceException < Exception; end
class ApiResponseService
RESPONSES_FILE_PATH = Rails.root.join("spec/fixtures/responses/responses.json")
@@responses_hash = nil
def self.response_body(object, key)
response_part(object, key, :body).to_json
end
def self.response_headers(object, key)
response_part(object, key, :headers)
end
private
def self.responses
unless @@responses_hash.present?
fd = File.open(RESPONSES_FILE_PATH, 'r')
@@responses_hash = HashWithIndifferentAccess.new(JSON.parse(fd.read))
end
@@responses_hash
end
def self.object_to_key(object)
class_name = case object.class.to_s
when "String"
object
when "Class"
object.name
else
object.class.name
end
class_name
end
def self.response_part(object, key, part = :body)
object_key = object_to_key(object)
raise ApiResponseServiceException.new("No #{object_key} in fixtures") unless responses.has_key?(object_key)
raise ApiResponseServiceException.new("No #{object_key}/#{key} in fixtures") unless responses[object_key].has_key?(key)
responses[object_key][key.to_s][part]
end
end
def api_response_fixture_body(object, key)
ApiResponseService.response_body(object.to_s, key)
end
def api_response_fixture_headers(object, key)
ApiResponseService.response_headers(object.to_s, key)
end
def api_response_fixture(object, key)
{body: api_response_fixture_body(object, key), headers: api_response_fixture_headers(object, key)}
end
end
RSpec.configure do |config|
config.include ApiResponseHelper
end

View File

@@ -0,0 +1,16 @@
module AuthenticationHelper
def sign_in_to_api(user)
request.headers.merge!(api_authentication_header(user))
end
def api_authentication_header(user)
token = Knock::AuthToken.new(payload: { sub: user.id }).token
{
'Authorization': "Bearer #{token}"
}
end
end
RSpec.configure do |config|
config.include AuthenticationHelper, type: :controller
end

View File

@@ -0,0 +1,21 @@
class Base64Image
attr_reader :data_uri
def initialize(data_uri)
@data_uri = data_uri
end
# Creates an instance from a given file path which is converted to a base64 data uri
def self.from_image(path, mime_type = "image/png")
data = Base64.encode64(File.open(path, "rb").read)
data_uri = "data:#{mime_type};base64,#{data}"
new(data_uri)
end
private
def encoded_image
@data_uri.split(",")[1]
end
end

15
spec/support/capybara.rb Normal file
View File

@@ -0,0 +1,15 @@
require 'capybara/rspec'
require 'capybara/rails'
Capybara.register_driver :selenium_chrome_headless_no_local_storage do |app|
Capybara::Selenium::Driver.load_selenium
browser_options = ::Selenium::WebDriver::Chrome::Options.new
browser_options.args << '--headless'
browser_options.args << '--disable-gpu' if Gem.win_platform?
Capybara::Selenium::Driver.new(app, browser: :chrome, options: browser_options, clear_local_storage: true, clear_session_storage: true)
end
#Capybara.javascript_driver = :selenium_chrome
Capybara.javascript_driver = :selenium_chrome_headless_no_local_storage
Capybara.server_port = AppHost.new.port

View File

@@ -0,0 +1,18 @@
module DropzoneHelper
def drop_file(file_path, type:)
page.execute_script <<-JS
fakeFileInput = window.$('<input/>').attr(
{id: 'fakeFileInput', type:'file'}
).appendTo('body');
JS
attach_file("fakeFileInput", file_path)
page.execute_script <<-JS
var e = jQuery.Event('drop', { dataTransfer : { files : [fakeFileInput.get(0).files[0]] } });
$("[data-behavior=#{type}]")[0].dropzone.listeners[0].events.drop(e);
JS
end
end
RSpec.configure do |config|
config.include DropzoneHelper
end

View File

@@ -0,0 +1,58 @@
module ExpansionHelper
def expand_section(name)
Section.new(self, name).expand
end
def section_expanded?(name)
Section.new(self, name).expanded?
end
def within_section(name)
Section.new(self, name).within_me do
yield
end
end
class Section
attr_reader :context, :name
def initialize(context, name)
@context = context
@name = name
end
def expand
return if expanded?
if element[:class].include? "collapse"
trigger.click
end
end
def expanded?
element[:class].include? "show"
end
def within_me
within "##{name}" do
yield
end
end
private
delegate :find, :within, to: :context
def element
find("##{name}", visible: :all)
end
def trigger
find("[data-target=\"##{name}\"]", visible: :all)
end
end
end
RSpec.configure do |config|
config.include ExpansionHelper
end

View File

@@ -0,0 +1,3 @@
RSpec.configure do |config|
config.include FactoryBot::Syntax::Methods
end

6
spec/support/locale.rb Normal file
View File

@@ -0,0 +1,6 @@
RSpec.configure do |config|
config.include AbstractController::Translation
config.before :each, type: :feature do
default_url_options[:locale] = I18n.default_locale
end
end

View File

@@ -0,0 +1,54 @@
require "rspec/expectations"
RSpec::Matchers.define :allow_content_type do |*content_types|
match do |record|
matcher.matches?(record, content_types)
end
chain :for do |attr_name|
matcher.for(attr_name)
end
chain :with_message do |message|
matcher.with_message(message)
end
private
def matcher
@matcher ||= AllowContentTypeMatcher.new
end
class AllowContentTypeMatcher
def for(attr_name)
@attr_name = attr_name
end
def with_message(message)
@message = message
end
def matches?(record, content_types)
Array.wrap(content_types).all? do |content_type|
record.send(attr_name).attach attachment_for(content_type)
record.valid?
!record.errors[attr_name].include? message
end
end
private
attr_reader :attr_name
def attachment_for(content_type)
suffix = content_type.to_s.split("/").last
{ io: StringIO.new("Hello world!"), filename: "test.#{suffix}", content_type: content_type }
end
def message
@message || I18n.translate("activerecord.errors.messages.content_type")
end
end
end
RSpec::Matchers.alias_matcher :allow_content_types, :allow_content_type

View File

@@ -0,0 +1,8 @@
require "rspec/expectations"
RSpec::Matchers.define :have_invalid_field do |field_name|
match do |page|
page.has_field?(name, class: "is-invalid") ||
page.find_field(field_name).native.attribute("validationMessage").present?
end
end

View File

@@ -0,0 +1,45 @@
require "rspec/expectations"
RSpec::Matchers.define :validate_attachment_of do |attr_name|
match do |record|
matcher.matches?(record, attr_name)
end
chain :on do |validation_context|
matcher.on(validation_context)
end
chain :with_message do |message|
matcher.with_message(message)
end
private
def matcher
@matcher ||= ValidateAttachmentOfMatcher.new
end
class ValidateAttachmentOfMatcher
def on(validation_context)
@validation_context = validation_context
end
def with_message(message)
@message = message
end
def matches?(record, attr_name)
record.send(attr_name).purge
record.valid?(validation_context)
record.errors[attr_name].include? message
end
private
attr_reader :validation_context
def message
@message || I18n.translate("activerecord.errors.messages.attached")
end
end
end

1
spec/support/money.rb Normal file
View File

@@ -0,0 +1 @@
require "money-rails/test_helpers"

9
spec/support/oath.rb Normal file
View File

@@ -0,0 +1,9 @@
Oath.test_mode!
RSpec.configure do |config|
config.include Oath::Test::ControllerHelpers, type: :controller
config.include Oath::Test::Helpers, type: :feature
config.after :each do
Oath.test_reset!
end
end

View File

@@ -0,0 +1,47 @@
module PdfReaderControllerHelper
def content_type
response.headers["Content-Type"]
end
def content_disposition
response.headers["Content-Disposition"]
end
def pdf_filename
content_disposition.scan(/filename="(.*)"/).last.first
end
def pdf_body
temp_pdf = Tempfile.new('pdf')
temp_pdf << response.body.force_encoding('UTF-8')
reader = PDF::Reader.new(temp_pdf)
reader.pages.map(&:text).join(" ").squish
end
end
module PdfReaderFeatureHelper
def content_type
response_headers["Content-Type"]
end
def content_disposition
response_headers["Content-Disposition"]
end
def pdf_filename
content_disposition.scan(/filename="(.*)"/).last.first
end
def pdf_body
temp_pdf = Tempfile.new('pdf')
temp_pdf << page.source.force_encoding('UTF-8')
reader = PDF::Reader.new(temp_pdf)
reader.pages.map(&:text).join(" ").squish
end
end
RSpec.configure do |config|
config.include PdfReaderControllerHelper, type: :controller
config.include PdfReaderFeatureHelper, type: :feature
end

1
spec/support/pundit.rb Normal file
View File

@@ -0,0 +1 @@
require "pundit/rspec"

View File

@@ -0,0 +1,7 @@
module Requests
module JsonHelpers
def json
JSON.parse(response.body)
end
end
end

View File

@@ -0,0 +1,9 @@
shared_examples_for "a contractable" do
it { is_expected.to respond_to(:contract) }
it { is_expected.to respond_to(:contract_file_name) }
describe "validations" do
it { is_expected.to allow_content_type("application/pdf").for(:contract) }
it { is_expected.not_to allow_content_types("text/plain", "image/png").for(:contract) }
end
end

View File

@@ -0,0 +1,15 @@
shared_examples_for "a freeformable" do
describe "#other?" do
context "when label is 'Other'" do
subject { described_class.new(label: "Other") }
it { is_expected.to be_other }
end
context "when label is not 'Other'" do
subject { described_class.new(label: "Not Other") }
it { is_expected.not_to be_other }
end
end
end

View File

@@ -0,0 +1,5 @@
shared_examples_for "a notable" do
describe "#notes" do
it { is_expected.to have_many(:notes).dependent(:destroy) }
end
end

View File

@@ -0,0 +1,11 @@
shared_examples_for "a photoable" do
it { is_expected.to respond_to(:photos) }
it { is_expected.to respond_to(:photo) }
describe "validations" do
describe "#photos" do
it { is_expected.to allow_content_type("image/png", "image/jpeg").for(:photos) }
it { is_expected.not_to allow_content_type("text/plain", "application/pdf").for(:photos) }
end
end
end

View File

@@ -0,0 +1,15 @@
shared_examples_for "a releasable" do
it { is_expected.to respond_to(:contact_person) }
it { is_expected.to respond_to(:release_number) }
it { is_expected.to respond_to(:uses_edl?) }
describe "associations" do
it { is_expected.to belong_to(:project).touch(true) }
it { is_expected.to belong_to(:contract_template).optional }
end
describe "#order_by_recent" do
subject { described_class }
it { is_expected.to respond_to(:order_by_recent) }
end
end

View File

@@ -0,0 +1,8 @@
shared_examples_for "a taggable" do
it { is_expected.to respond_to(:tags) }
it { is_expected.to respond_to(:internal_tags) }
describe "#taggable_status" do
it { is_expected.to define_enum_for(:tagging_status).with_values([:pending, :started, :finished, :failed]).with_prefix(:tagging) }
end
end

View File

@@ -0,0 +1,6 @@
shared_examples_for 'a worksheet' do
it { is_expected.to respond_to(:title) }
it { is_expected.to respond_to(:fill_content) }
it { is_expected.to respond_to(:format) }
it { is_expected.to respond_to(:style) }
end

View File

@@ -0,0 +1,37 @@
shared_examples_for "an exploitable" do
Exploitable::FIELDS.each do |field|
it { is_expected.to belong_to(field).optional }
it { is_expected.to respond_to("#{field}_text") }
describe "#{field}_value" do
context "when a pre-defined value is chosen" do
it "returns the label for that value" do
exploitable = build(described_class.model_name.singular,
field => build(field, label: "My Field"),
"#{field}_text" => "Test")
expect(exploitable.public_send("#{field}_value")).to eq "My Field"
end
end
context "when 'Other' is chosen" do
it "returns the #{field} text" do
exploitable = build(described_class.model_name.singular,
field => build(field, label: "Other"),
"#{field}_text" => "Test")
expect(exploitable.public_send("#{field}_value")).to eq "Test"
end
end
if described_class.respond_to?(:contract_template)
context "when a contract template is present" do
it "returns the #{field} value from the contract template" do
contract_template = build(:contract_template, field => build(field, label: "Contract Template Field"))
exploitable = build(described_class.model_name.singular, contract_template: contract_template)
expect(exploitable.public_send("#{field}_value")).to eq "Contract Template Field"
end
end
end
end
end
end

View File

@@ -0,0 +1,6 @@
Shoulda::Matchers.configure do |config|
config.integrate do |with|
with.test_framework :rspec
with.library :rails
end
end

View File

@@ -0,0 +1,12 @@
module SignatureHelper
def draw_signature(file, signature_field_id)
data_uri = ActionController::Base.helpers.escape_javascript(Base64Image.from_image(file).data_uri)
page.execute_script <<-JS
$("##{signature_field_id}").val("#{data_uri}")
JS
end
end
RSpec.configure do |config|
config.include SignatureHelper
end

6
spec/support/storage.rb Normal file
View File

@@ -0,0 +1,6 @@
# Clean up files stored during testing
RSpec.configure do |config|
config.after :suite do
FileUtils.rm_rf Rails.root.join("tmp/storage")
end
end

View File

@@ -0,0 +1,3 @@
RSpec.configure do |config|
config.include ActiveSupport::Testing::TimeHelpers
end

2
spec/support/webmock.rb Normal file
View File

@@ -0,0 +1,2 @@
require 'webmock/rspec'
WebMock.allow_net_connect!

View File

@@ -0,0 +1,16 @@
module WindowSizeHelper
def resize_window_to(width, height)
old_size = Capybara.current_session.current_window.size
Capybara.current_session.current_window.resize_to(width, height)
yield
Capybara.current_session.current_window.resize_to(*old_size)
end
def set_window_size_permanently_to(width, height)
Capybara.current_session.current_window.resize_to(width, height)
end
end
RSpec.configure do |config|
config.include WindowSizeHelper, type: :feature
end