Skip to content

Commit

Permalink
Added tests for export feature
Browse files Browse the repository at this point in the history
  • Loading branch information
lucaguindani committed Jun 26, 2018
1 parent 73e4351 commit 92a6c60
Show file tree
Hide file tree
Showing 10 changed files with 154 additions and 25 deletions.
3 changes: 3 additions & 0 deletions app/mailers/export_mailer.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
class ExportMailer < ApplicationMailer
def send_message(export)
I18n.locale = export.user.primary_language
@export = export

mail(
Expand All @@ -8,5 +9,7 @@ def send_message(export)
:from => "[email protected]",
:template_name => "export"
)
ensure
I18n.locale = I18n.default_locale
end
end
4 changes: 2 additions & 2 deletions app/models/export.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ class Export < ActiveRecord::Base
belongs_to :user
belongs_to :catalog

validates_presence_of :user_id
validates_presence_of :catalog_id
validates_presence_of :user
validates_presence_of :catalog

validates_inclusion_of :category, :in => CATEGORY_OPTIONS
validates_inclusion_of :status, :in => STATUS_OPTIONS
Expand Down
1 change: 0 additions & 1 deletion app/views/export_mailer/export.html.erb
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
<p><%= t("common.greeting") %>,</p>

<%# TODO: add translations %>
<p>
<div>
<%= t(".export_available", :category => @export.category) %>
Expand Down
7 changes: 0 additions & 7 deletions test/controllers/exports_controller_test.rb

This file was deleted.

37 changes: 31 additions & 6 deletions test/fixtures/exports.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,51 @@
#
# Table name: exports
#
# id :integer not null, primary key
# user_id :integer
# catalog_id :integer
# category :string
# created_at :datetime not null
# file :boolean
# id :integer not null, primary key
# status :string
# updated_at :datetime not null
# user_id :integer
# created_at :datetime not null
#

# Read about fixtures at http://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html

one:
user: one_admin
catalog: one
type: "Export::Catima"
category: "catima"
status: "ready"
file: true

two:
one_expired:
user: one_admin
catalog: one
type: "Export::Catima"
category: "catima"
status: "ready"
file: false
created_at: <%= Time.zone.now - 8.days %>
updated_at: <%= Time.zone.now - 8.days %>

one_processing:
user: one_admin
catalog: one
category: "catima"
status: "processing"
file: false

one_error:
user: one_admin
catalog: one
category: "catima"
status: "error"
file: false

two:
user: two_admin
catalog: two
category: "catima"
status: "ready"
file: true
17 changes: 17 additions & 0 deletions test/integration/catalog_admin/exports_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
require "test_helper"

class CatalogAdmin::ExportsTest < ActionDispatch::IntegrationTest
test "create a catima export" do
log_in_as("[email protected]", "password")
visit("/two/en/admin/_exports")

assert_difference("Export.count", + 1) do
first("button", :text => "New export (choose format)").click
first("a", :text => "Catima").click
end

assert(page.has_content?("[email protected]"))
assert(page.has_content?("processing"))
assert(page.has_content?("valid"))
end
end
26 changes: 26 additions & 0 deletions test/mailers/previews/export_mailer_preview.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,29 @@
# Preview all emails at http://localhost:3000/rails/mailers/export_mailer
class ExportMailerPreview < ActionMailer::Preview
%w(de en fr it).each do |locale|
# Preview this email at
# http://localhost:3000/rails/mailers/export_mailer/export_available_en
define_method("export_available_#{locale}") do
catalog = Catalog.first_or_create!(
:name => "Sample",
:slug => "sample",
:primary_language => "en"
)
user = User.new(
:email => "[email protected]",
:primary_language => "en"
)
export = Export.new(
:id => 1,
:user => user,
:catalog => catalog,
:category => "catima",
:status => "ready",
:file => true,
:created_at => Time.zone.now,
:updated_at => Time.zone.now
)
ExportMailer.send_message(export)
end
end
end
28 changes: 25 additions & 3 deletions test/models/export_test.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,29 @@
require 'test_helper'

class ExportTest < ActiveSupport::TestCase
# test "the truth" do
# assert true
# end
should validate_presence_of(:user)
should validate_presence_of(:catalog)
should validate_inclusion_of(:category).in_array(%w(catima))
should validate_inclusion_of(:status).in_array(%w(error processing ready))

test "export ready" do
export = exports(:one)
assert(export.ready?)
assert(export.validity?)
assert(export.file?)
end

test "export error but valid" do
export = exports(:one_error)
refute(export.ready?)
assert(export.validity?)
refute(export.file?)
end

test "export expired" do
export = exports(:one_expired)
assert(export.ready?)
refute(export.validity?)
refute(export.file?)
end
end
50 changes: 50 additions & 0 deletions test/policies/export_policy_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
require "test_helper"

class ExportPolicyTest < ActiveSupport::TestCase
test "#create? allows admins of the catalog" do
refute(policy(Guest.new, exports(:one)).create?)
refute(policy(users(:one), exports(:one)).create?)
refute(policy(users(:one_member), exports(:one)).create?)
refute(policy(users(:one_editor), exports(:one)).create?)
refute(policy(users(:one_super_editor), exports(:one)).create?)
refute(policy(users(:one_reviewer), exports(:one)).create?)
refute(policy(users(:one_admin), exports(:two)).create?)
assert(policy(users(:one_admin), exports(:one)).create?)
assert(policy(users(:system_admin), exports(:one)).create?)
end

test "#download? allows admins of the catalog" do
refute(policy(Guest.new, exports(:one)).download?)
refute(policy(users(:one), exports(:one)).download?)
refute(policy(users(:one_member), exports(:one)).download?)
refute(policy(users(:one_editor), exports(:one)).download?)
refute(policy(users(:one_super_editor), exports(:one)).download?)
refute(policy(users(:one_reviewer), exports(:one)).download?)
refute(policy(users(:one_admin), exports(:two)).download?)
assert(policy(users(:one_admin), exports(:one)).download?)
assert(policy(users(:system_admin), exports(:one)).download?)
end

test "#download? only if export is valid, ready and has file" do
refute(policy(users(:one_admin), exports(:one_expired)).download?)
refute(policy(users(:one_admin), exports(:one_processing)).download?)
refute(policy(users(:one_admin), exports(:one_error)).download?)
assert(policy(users(:one_admin), exports(:one)).download?)
end

test "#index? allows admins of the catalog" do
refute(policy(Guest.new, exports(:one)).index?)
refute(policy(users(:one), exports(:one)).index?)
refute(policy(users(:one_member), exports(:one)).index?)
refute(policy(users(:one_editor), exports(:one)).index?)
refute(policy(users(:one_super_editor), exports(:one)).index?)
refute(policy(users(:one_reviewer), exports(:one)).index?)
refute(policy(users(:one_admin), exports(:two)).index?)
assert(policy(users(:one_admin), exports(:one)).index?)
assert(policy(users(:system_admin), exports(:one)).index?)
end

def policy(user, export=exports(:one))
ExportPolicy.new(user, export)
end
end
6 changes: 0 additions & 6 deletions test/workers/export_worker_test.rb

This file was deleted.

0 comments on commit 92a6c60

Please sign in to comment.