1
0
mirror of https://github.com/LaCasemate/fab-manager.git synced 2025-01-18 07:52:23 +01:00

improved & fixed test suite

This commit is contained in:
Sylvain 2018-12-12 13:49:14 +01:00
parent f2eaf4e4e1
commit 811b7193f7
10 changed files with 421 additions and 290 deletions

View File

@ -1,5 +1,11 @@
# Changelog Fab Manager
- Refactored subscriptions to keep track of the previous ones
- Improved automated tests suite
- Added Rubocop gem to the Gemfile
- [TODO DEPLOY] `rake db:migrate`
- [TODO DEPLOY] `bundle install`
# v2.7.4 2018 December 04
- Applied Rubocop rules to some ruby files

View File

@ -1,4 +1,4 @@
class Component < ActiveRecord::Base
has_and_belongs_to_many :projects, join_table: :projects_components
has_and_belongs_to_many :projects, join_table: 'projects_components'
validates :name, presence: true, length: { maximum: 50 }
end
end

View File

@ -9,7 +9,7 @@ class Event < ActiveRecord::Base
belongs_to :category
validates :category, presence: true
has_many :reservations, as: :reservable, dependent: :destroy
has_and_belongs_to_many :event_themes, join_table: :events_event_themes, dependent: :destroy
has_and_belongs_to_many :event_themes, join_table: 'events_event_themes', dependent: :destroy
has_many :event_price_categories, dependent: :destroy
has_many :price_categories, through: :event_price_categories

View File

@ -5,7 +5,7 @@ class Training < ActiveRecord::Base
has_one :training_image, as: :viewable, dependent: :destroy
accepts_nested_attributes_for :training_image, allow_destroy: true
has_and_belongs_to_many :machines, join_table: :trainings_machines
has_and_belongs_to_many :machines, join_table: 'trainings_machines'
has_many :trainings_availabilities
has_many :availabilities, through: :trainings_availabilities, dependent: :destroy
@ -32,18 +32,19 @@ class Training < ActiveRecord::Base
def create_statistic_subtype
index = StatisticIndex.where(es_type_key: 'training')
StatisticSubType.create!({statistic_types: index.first.statistic_types, key: self.slug, label: self.name})
StatisticSubType.create!(statistic_types: index.first.statistic_types, key: slug, label: name)
end
def update_statistic_subtype
index = StatisticIndex.where(es_type_key: 'training')
subtype = StatisticSubType.joins(statistic_type_sub_types: :statistic_type).where(key: self.slug, statistic_types: { statistic_index_id: index.first.id }).first
subtype.label = self.name
subtype = StatisticSubType.joins(statistic_type_sub_types: :statistic_type)
.where(key: slug, statistic_types: { statistic_index_id: index.first.id }).first
subtype.label = name
subtype.save!
end
def remove_statistic_subtype
subtype = StatisticSubType.where(key: self.slug).first
subtype = StatisticSubType.where(key: slug).first
subtype.destroy!
end
@ -52,9 +53,10 @@ class Training < ActiveRecord::Base
end
private
def create_trainings_pricings
Group.all.each do |group|
TrainingsPricing.create(training: self, group: group, amount: 0)
end
def create_trainings_pricings
Group.all.each do |group|
TrainingsPricing.create(training: self, group: group, amount: 0)
end
end
end

View File

@ -15,8 +15,8 @@ ActiveRecord::Schema.define(version: 20181210105917) do
# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"
enable_extension "unaccent"
enable_extension "pg_trgm"
enable_extension "unaccent"
create_table "abuses", force: :cascade do |t|
t.integer "signaled_id"
@ -32,14 +32,14 @@ ActiveRecord::Schema.define(version: 20181210105917) do
add_index "abuses", ["signaled_type", "signaled_id"], name: "index_abuses_on_signaled_type_and_signaled_id", using: :btree
create_table "addresses", force: :cascade do |t|
t.string "address"
t.string "street_number"
t.string "route"
t.string "locality"
t.string "country"
t.string "postal_code"
t.string "address", limit: 255
t.string "street_number", limit: 255
t.string "route", limit: 255
t.string "locality", limit: 255
t.string "country", limit: 255
t.string "postal_code", limit: 255
t.integer "placeable_id"
t.string "placeable_type"
t.string "placeable_type", limit: 255
t.datetime "created_at"
t.datetime "updated_at"
end
@ -55,9 +55,9 @@ ActiveRecord::Schema.define(version: 20181210105917) do
create_table "assets", force: :cascade do |t|
t.integer "viewable_id"
t.string "viewable_type"
t.string "attachment"
t.string "type"
t.string "viewable_type", limit: 255
t.string "attachment", limit: 255
t.string "type", limit: 255
t.datetime "created_at"
t.datetime "updated_at"
end
@ -74,12 +74,12 @@ ActiveRecord::Schema.define(version: 20181210105917) do
create_table "availabilities", force: :cascade do |t|
t.datetime "start_at"
t.datetime "end_at"
t.string "available_type"
t.string "available_type", limit: 255
t.datetime "created_at"
t.datetime "updated_at"
t.integer "nb_total_places"
t.boolean "destroying", default: false
t.boolean "lock", default: false
t.boolean "destroying", default: false
t.boolean "lock", default: false
end
create_table "availability_tags", force: :cascade do |t|
@ -93,7 +93,7 @@ ActiveRecord::Schema.define(version: 20181210105917) do
add_index "availability_tags", ["tag_id"], name: "index_availability_tags_on_tag_id", using: :btree
create_table "categories", force: :cascade do |t|
t.string "name"
t.string "name", limit: 255
t.datetime "created_at"
t.datetime "updated_at"
t.string "slug"
@ -102,7 +102,7 @@ ActiveRecord::Schema.define(version: 20181210105917) do
add_index "categories", ["slug"], name: "index_categories_on_slug", unique: true, using: :btree
create_table "components", force: :cascade do |t|
t.string "name", null: false
t.string "name", limit: 255, null: false
end
create_table "coupons", force: :cascade do |t|
@ -120,7 +120,7 @@ ActiveRecord::Schema.define(version: 20181210105917) do
create_table "credits", force: :cascade do |t|
t.integer "creditable_id"
t.string "creditable_type"
t.string "creditable_type", limit: 255
t.integer "plan_id"
t.integer "hours"
t.datetime "created_at"
@ -161,7 +161,7 @@ ActiveRecord::Schema.define(version: 20181210105917) do
add_index "event_themes", ["slug"], name: "index_event_themes_on_slug", unique: true, using: :btree
create_table "events", force: :cascade do |t|
t.string "title"
t.string "title", limit: 255
t.text "description"
t.datetime "created_at"
t.datetime "updated_at"
@ -199,10 +199,10 @@ ActiveRecord::Schema.define(version: 20181210105917) do
add_index "exports", ["user_id"], name: "index_exports_on_user_id", using: :btree
create_table "friendly_id_slugs", force: :cascade do |t|
t.string "slug", null: false
t.integer "sluggable_id", null: false
t.string "slug", limit: 255, null: false
t.integer "sluggable_id", null: false
t.string "sluggable_type", limit: 50
t.string "scope"
t.string "scope", limit: 255
t.datetime "created_at"
end
@ -212,10 +212,10 @@ ActiveRecord::Schema.define(version: 20181210105917) do
add_index "friendly_id_slugs", ["sluggable_type"], name: "index_friendly_id_slugs_on_sluggable_type", using: :btree
create_table "groups", force: :cascade do |t|
t.string "name"
t.string "name", limit: 255
t.datetime "created_at"
t.datetime "updated_at"
t.string "slug"
t.string "slug", limit: 255
t.boolean "disabled"
end
@ -223,7 +223,7 @@ ActiveRecord::Schema.define(version: 20181210105917) do
create_table "invoice_items", force: :cascade do |t|
t.integer "invoice_id"
t.string "stp_invoice_item_id"
t.string "stp_invoice_item_id", limit: 255
t.integer "amount"
t.datetime "created_at"
t.datetime "updated_at"
@ -236,17 +236,17 @@ ActiveRecord::Schema.define(version: 20181210105917) do
create_table "invoices", force: :cascade do |t|
t.integer "invoiced_id"
t.string "invoiced_type"
t.string "stp_invoice_id"
t.string "invoiced_type", limit: 255
t.string "stp_invoice_id", limit: 255
t.integer "total"
t.datetime "created_at"
t.datetime "updated_at"
t.integer "user_id"
t.string "reference"
t.string "avoir_mode"
t.string "reference", limit: 255
t.string "avoir_mode", limit: 255
t.datetime "avoir_date"
t.integer "invoice_id"
t.string "type"
t.string "type", limit: 255
t.boolean "subscription_to_expire"
t.text "description"
t.integer "wallet_amount"
@ -260,17 +260,17 @@ ActiveRecord::Schema.define(version: 20181210105917) do
add_index "invoices", ["wallet_transaction_id"], name: "index_invoices_on_wallet_transaction_id", using: :btree
create_table "licences", force: :cascade do |t|
t.string "name", null: false
t.string "name", limit: 255, null: false
t.text "description"
end
create_table "machines", force: :cascade do |t|
t.string "name", null: false
t.string "name", limit: 255, null: false
t.text "description"
t.text "spec"
t.datetime "created_at"
t.datetime "updated_at"
t.string "slug"
t.string "slug", limit: 255
t.boolean "disabled"
end
@ -287,14 +287,14 @@ ActiveRecord::Schema.define(version: 20181210105917) do
create_table "notifications", force: :cascade do |t|
t.integer "receiver_id"
t.integer "attached_object_id"
t.string "attached_object_type"
t.string "attached_object_type", limit: 255
t.integer "notification_type_id"
t.boolean "is_read", default: false
t.boolean "is_read", default: false
t.datetime "created_at"
t.datetime "updated_at"
t.string "receiver_type"
t.boolean "is_send", default: false
t.jsonb "meta_data", default: {}
t.boolean "is_send", default: false
t.jsonb "meta_data", default: {}
end
add_index "notifications", ["notification_type_id"], name: "index_notifications_on_notification_type_id", using: :btree
@ -363,20 +363,20 @@ ActiveRecord::Schema.define(version: 20181210105917) do
add_index "organizations", ["profile_id"], name: "index_organizations_on_profile_id", using: :btree
create_table "plans", force: :cascade do |t|
t.string "name"
t.string "name", limit: 255
t.integer "amount"
t.string "interval"
t.string "interval", limit: 255
t.integer "group_id"
t.string "stp_plan_id"
t.string "stp_plan_id", limit: 255
t.datetime "created_at"
t.datetime "updated_at"
t.integer "training_credit_nb", default: 0
t.boolean "is_rolling", default: true
t.integer "training_credit_nb", default: 0
t.boolean "is_rolling", default: true
t.text "description"
t.string "type"
t.string "base_name"
t.integer "ui_weight", default: 0
t.integer "interval_count", default: 1
t.integer "ui_weight", default: 0
t.integer "interval_count", default: 1
t.string "slug"
t.boolean "disabled"
end
@ -406,11 +406,11 @@ ActiveRecord::Schema.define(version: 20181210105917) do
create_table "profiles", force: :cascade do |t|
t.integer "user_id"
t.string "first_name"
t.string "last_name"
t.string "first_name", limit: 255
t.string "last_name", limit: 255
t.boolean "gender"
t.date "birthday"
t.string "phone"
t.string "phone", limit: 255
t.text "interest"
t.text "software_mastered"
t.datetime "created_at"
@ -440,7 +440,7 @@ ActiveRecord::Schema.define(version: 20181210105917) do
t.integer "project_id"
t.datetime "created_at"
t.datetime "updated_at"
t.string "title"
t.string "title", limit: 255
t.integer "step_nb"
end
@ -451,27 +451,27 @@ ActiveRecord::Schema.define(version: 20181210105917) do
t.integer "user_id"
t.datetime "created_at"
t.datetime "updated_at"
t.boolean "is_valid", default: false
t.string "valid_token"
t.boolean "is_valid", default: false
t.string "valid_token", limit: 255
end
add_index "project_users", ["project_id"], name: "index_project_users_on_project_id", using: :btree
add_index "project_users", ["user_id"], name: "index_project_users_on_user_id", using: :btree
create_table "projects", force: :cascade do |t|
t.string "name"
t.string "name", limit: 255
t.text "description"
t.datetime "created_at"
t.datetime "updated_at"
t.integer "author_id"
t.text "tags"
t.integer "licence_id"
t.string "state"
t.string "slug"
t.string "state", limit: 255
t.string "slug", limit: 255
t.datetime "published_at"
end
add_index "projects", ["slug"], name: "index_projects_on_slug", unique: true, using: :btree
add_index "projects", ["slug"], name: "index_projects_on_slug", using: :btree
create_table "projects_components", force: :cascade do |t|
t.integer "project_id"
@ -511,19 +511,19 @@ ActiveRecord::Schema.define(version: 20181210105917) do
t.datetime "created_at"
t.datetime "updated_at"
t.integer "reservable_id"
t.string "reservable_type"
t.string "stp_invoice_id"
t.string "reservable_type", limit: 255
t.string "stp_invoice_id", limit: 255
t.integer "nb_reserve_places"
end
add_index "reservations", ["reservable_type", "reservable_id"], name: "index_reservations_on_reservable_type_and_reservable_id", using: :btree
add_index "reservations", ["reservable_id", "reservable_type"], name: "index_reservations_on_reservable_id_and_reservable_type", using: :btree
add_index "reservations", ["stp_invoice_id"], name: "index_reservations_on_stp_invoice_id", using: :btree
add_index "reservations", ["user_id"], name: "index_reservations_on_user_id", using: :btree
create_table "roles", force: :cascade do |t|
t.string "name"
t.string "name", limit: 255
t.integer "resource_id"
t.string "resource_type"
t.string "resource_type", limit: 255
t.datetime "created_at"
t.datetime "updated_at"
end
@ -598,18 +598,18 @@ ActiveRecord::Schema.define(version: 20181210105917) do
create_table "statistic_fields", force: :cascade do |t|
t.integer "statistic_index_id"
t.string "key"
t.string "label"
t.string "key", limit: 255
t.string "label", limit: 255
t.datetime "created_at"
t.datetime "updated_at"
t.string "data_type"
t.string "data_type", limit: 255
end
add_index "statistic_fields", ["statistic_index_id"], name: "index_statistic_fields_on_statistic_index_id", using: :btree
create_table "statistic_graphs", force: :cascade do |t|
t.integer "statistic_index_id"
t.string "chart_type"
t.string "chart_type", limit: 255
t.integer "limit"
t.datetime "created_at"
t.datetime "updated_at"
@ -618,17 +618,17 @@ ActiveRecord::Schema.define(version: 20181210105917) do
add_index "statistic_graphs", ["statistic_index_id"], name: "index_statistic_graphs_on_statistic_index_id", using: :btree
create_table "statistic_indices", force: :cascade do |t|
t.string "es_type_key"
t.string "label"
t.string "es_type_key", limit: 255
t.string "label", limit: 255
t.datetime "created_at"
t.datetime "updated_at"
t.boolean "table", default: true
t.boolean "ca", default: true
t.boolean "table", default: true
t.boolean "ca", default: true
end
create_table "statistic_sub_types", force: :cascade do |t|
t.string "key"
t.string "label"
t.string "key", limit: 255
t.string "label", limit: 255
t.datetime "created_at"
t.datetime "updated_at"
end
@ -645,8 +645,8 @@ ActiveRecord::Schema.define(version: 20181210105917) do
create_table "statistic_types", force: :cascade do |t|
t.integer "statistic_index_id"
t.string "key"
t.string "label"
t.string "key", limit: 255
t.string "label", limit: 255
t.boolean "graph"
t.datetime "created_at"
t.datetime "updated_at"
@ -664,7 +664,7 @@ ActiveRecord::Schema.define(version: 20181210105917) do
create_table "subscriptions", force: :cascade do |t|
t.integer "plan_id"
t.integer "user_id"
t.string "stp_subscription_id"
t.string "stp_subscription_id", limit: 255
t.datetime "created_at"
t.datetime "updated_at"
t.datetime "expiration_date"
@ -683,7 +683,7 @@ ActiveRecord::Schema.define(version: 20181210105917) do
add_index "tags", ["name"], name: "index_tags_on_name", unique: true, using: :btree
create_table "themes", force: :cascade do |t|
t.string "name", null: false
t.string "name", limit: 255, null: false
end
create_table "tickets", force: :cascade do |t|
@ -698,13 +698,13 @@ ActiveRecord::Schema.define(version: 20181210105917) do
add_index "tickets", ["reservation_id"], name: "index_tickets_on_reservation_id", using: :btree
create_table "trainings", force: :cascade do |t|
t.string "name"
t.string "name", limit: 255
t.datetime "created_at"
t.datetime "updated_at"
t.integer "nb_total_places"
t.string "slug"
t.string "slug", limit: 255
t.text "description"
t.boolean "public_page", default: true
t.boolean "public_page", default: true
t.boolean "disabled"
end
@ -760,32 +760,32 @@ ActiveRecord::Schema.define(version: 20181210105917) do
add_index "user_trainings", ["user_id"], name: "index_user_trainings_on_user_id", using: :btree
create_table "users", force: :cascade do |t|
t.string "email", default: "", null: false
t.string "encrypted_password", default: "", null: false
t.string "reset_password_token"
t.string "email", limit: 255, default: "", null: false
t.string "encrypted_password", limit: 255, default: "", null: false
t.string "reset_password_token", limit: 255
t.datetime "reset_password_sent_at"
t.datetime "remember_created_at"
t.integer "sign_in_count", default: 0, null: false
t.integer "sign_in_count", default: 0, null: false
t.datetime "current_sign_in_at"
t.datetime "last_sign_in_at"
t.string "current_sign_in_ip"
t.string "last_sign_in_ip"
t.string "confirmation_token"
t.string "current_sign_in_ip", limit: 255
t.string "last_sign_in_ip", limit: 255
t.string "confirmation_token", limit: 255
t.datetime "confirmed_at"
t.datetime "confirmation_sent_at"
t.string "unconfirmed_email"
t.integer "failed_attempts", default: 0, null: false
t.string "unlock_token"
t.string "unconfirmed_email", limit: 255
t.integer "failed_attempts", default: 0, null: false
t.string "unlock_token", limit: 255
t.datetime "locked_at"
t.datetime "created_at"
t.datetime "updated_at"
t.boolean "is_allow_contact", default: true
t.boolean "is_allow_contact", default: true
t.integer "group_id"
t.string "stp_customer_id"
t.string "username"
t.string "slug"
t.boolean "is_active", default: true
t.boolean "invoicing_disabled", default: false
t.string "stp_customer_id", limit: 255
t.string "username", limit: 255
t.string "slug", limit: 255
t.boolean "is_active", default: true
t.boolean "invoicing_disabled", default: false
t.string "provider"
t.string "uid"
t.string "auth_token"

View File

@ -1,6 +1,6 @@
{
"name": "fab-manager",
"version": "2.7.4",
"version": "2.7.5-dev",
"description": "FabManager is the FabLab management solution. It is web-based, open-source and totally free.",
"keywords": [
"fablab",

View File

@ -7,7 +7,7 @@ module Reservations
login_as(@admin, scope: :user)
end
test "user without subscription and with invoicing disabled reserves a machine with success" do
test 'user without subscription and with invoicing disabled reserves a machine with success' do
@user_without_subscription.update!(invoicing_disabled: true)
machine = Machine.find(6)
availability = machine.availabilities.first
@ -18,16 +18,17 @@ module Reservations
users_credit_count = UsersCredit.count
post reservations_path, { reservation: {
user_id: @user_without_subscription.id,
reservable_id: machine.id,
reservable_type: machine.class.name,
slots_attributes: [
{ start_at: availability.start_at.to_s(:iso8601),
end_at: (availability.start_at + 1.hour).to_s(:iso8601),
availability_id: availability.id
}
]
}}.to_json, default_headers
user_id: @user_without_subscription.id,
reservable_id: machine.id,
reservable_type: machine.class.name,
slots_attributes: [
{
start_at: availability.start_at.to_s(:iso8601),
end_at: (availability.start_at + 1.hour).to_s(:iso8601),
availability_id: availability.id
}
]
} }.to_json, default_headers
# general assertions
assert_equal 201, response.status
@ -36,6 +37,9 @@ module Reservations
assert_equal invoice_items_count, InvoiceItem.count
assert_equal users_credit_count, UsersCredit.count
# subscription assertions
assert_nil @user_without_subscription.subscribed_plan
# reservation assertions
reservation = Reservation.last
@ -46,7 +50,7 @@ module Reservations
assert_not_empty Notification.where(attached_object: reservation)
end
test "user without subscription reserves a machine with success" do
test 'user without subscription reserves a machine with success' do
machine = Machine.find(6)
availability = machine.availabilities.first
@ -56,16 +60,17 @@ module Reservations
users_credit_count = UsersCredit.count
post reservations_path, { reservation: {
user_id: @user_without_subscription.id,
reservable_id: machine.id,
reservable_type: machine.class.name,
slots_attributes: [
{ start_at: availability.start_at.to_s(:iso8601),
end_at: (availability.start_at + 1.hour).to_s(:iso8601),
availability_id: availability.id
}
]
}}.to_json, default_headers
user_id: @user_without_subscription.id,
reservable_id: machine.id,
reservable_type: machine.class.name,
slots_attributes: [
{
start_at: availability.start_at.to_s(:iso8601),
end_at: (availability.start_at + 1.hour).to_s(:iso8601),
availability_id: availability.id
}
]
} }.to_json, default_headers
# general assertions
assert_equal 201, response.status
@ -74,6 +79,9 @@ module Reservations
assert_equal invoice_items_count + 1, InvoiceItem.count
assert_equal users_credit_count, UsersCredit.count
# subscription assertions
assert_nil @user_without_subscription.subscribed_plan
# reservation assertions
reservation = Reservation.last
@ -101,7 +109,7 @@ module Reservations
assert_not_empty Notification.where(attached_object: reservation)
end
test "user without subscription reserves a training with success" do
test 'user without subscription reserves a training with success' do
training = Training.first
availability = training.availabilities.first
@ -110,16 +118,17 @@ module Reservations
invoice_items_count = InvoiceItem.count
post reservations_path, { reservation: {
user_id: @user_without_subscription.id,
reservable_id: training.id,
reservable_type: training.class.name,
slots_attributes: [
{ start_at: availability.start_at.to_s(:iso8601),
end_at: (availability.start_at + 1.hour).to_s(:iso8601),
availability_id: availability.id
}
]
}}.to_json, default_headers
user_id: @user_without_subscription.id,
reservable_id: training.id,
reservable_type: training.class.name,
slots_attributes: [
{
start_at: availability.start_at.to_s(:iso8601),
end_at: (availability.start_at + 1.hour).to_s(:iso8601),
availability_id: availability.id
}
]
} }.to_json, default_headers
# general assertions
assert_equal 201, response.status
@ -127,6 +136,9 @@ module Reservations
assert_equal invoice_count + 1, Invoice.count
assert_equal invoice_items_count + 1, InvoiceItem.count
# subscription assertions
assert_nil @user_without_subscription.subscribed_plan
# reservation assertions
reservation = Reservation.last
@ -153,7 +165,7 @@ module Reservations
assert_not_empty Notification.where(attached_object: reservation)
end
test "user with subscription reserves a machine with success" do
test 'user with subscription reserves a machine with success' do
plan = @user_with_subscription.subscribed_plan
machine = Machine.find(6)
availability = machine.availabilities.first
@ -164,20 +176,22 @@ module Reservations
users_credit_count = UsersCredit.count
post reservations_path, { reservation: {
user_id: @user_with_subscription.id,
reservable_id: machine.id,
reservable_type: machine.class.name,
slots_attributes: [
{ start_at: availability.start_at.to_s(:iso8601),
end_at: (availability.start_at + 1.hour).to_s(:iso8601),
availability_id: availability.id
},
{ start_at: (availability.start_at + 1.hour).to_s(:iso8601),
end_at: (availability.start_at + 2.hours).to_s(:iso8601),
availability_id: availability.id
}
]
}}.to_json, default_headers
user_id: @user_with_subscription.id,
reservable_id: machine.id,
reservable_type: machine.class.name,
slots_attributes: [
{
start_at: availability.start_at.to_s(:iso8601),
end_at: (availability.start_at + 1.hour).to_s(:iso8601),
availability_id: availability.id
},
{
start_at: (availability.start_at + 1.hour).to_s(:iso8601),
end_at: (availability.start_at + 2.hours).to_s(:iso8601),
availability_id: availability.id
}
]
} }.to_json, default_headers
# general assertions
assert_equal 201, response.status
@ -186,6 +200,10 @@ module Reservations
assert_equal invoice_items_count + 2, InvoiceItem.count
assert_equal users_credit_count + 1, UsersCredit.count
# subscription assertions
assert_not_nil @user_with_subscription.subscribed_plan
assert_equal plan.id, @user_with_subscription.subscribed_plan.id
# reservation assertions
reservation = Reservation.last
@ -203,9 +221,9 @@ module Reservations
invoice_items = InvoiceItem.last(2)
machine_price = machine.prices.find_by(group_id: @user_with_subscription.group_id, plan_id: plan.id).amount
assert invoice_items.any? { |invoice| invoice.amount == 0 }
assert invoice_items.any? { |invoice| invoice.amount == machine_price }
assert invoice_items.all? { |invoice| invoice.stp_invoice_item_id.blank? }
assert(invoice_items.any? { |ii| ii.amount.zero? })
assert(invoice_items.any? { |ii| ii.amount == machine_price })
assert(invoice_items.all? { |ii| ii.stp_invoice_item_id.blank? })
# users_credits assertions
users_credit = UsersCredit.last
@ -221,7 +239,7 @@ module Reservations
assert_not_empty Notification.where(attached_object: reservation)
end
test "user without subscription reserves a machine and pay by wallet with success" do
test 'user without subscription reserves a machine and pay by wallet with success' do
@vlonchamp = User.find_by(username: 'vlonchamp')
machine = Machine.find(6)
availability = machine.availabilities.first
@ -232,16 +250,17 @@ module Reservations
users_credit_count = UsersCredit.count
post reservations_path, { reservation: {
user_id: @vlonchamp.id,
reservable_id: machine.id,
reservable_type: machine.class.name,
slots_attributes: [
{ start_at: availability.start_at.to_s(:iso8601),
end_at: (availability.start_at + 1.hour).to_s(:iso8601),
availability_id: availability.id
}
]
}}.to_json, default_headers
user_id: @vlonchamp.id,
reservable_id: machine.id,
reservable_type: machine.class.name,
slots_attributes: [
{
start_at: availability.start_at.to_s(:iso8601),
end_at: (availability.start_at + 1.hour).to_s(:iso8601),
availability_id: availability.id
}
]
} }.to_json, default_headers
# general assertions
assert_equal 201, response.status
@ -250,6 +269,9 @@ module Reservations
assert_equal invoice_items_count + 1, InvoiceItem.count
assert_equal users_credit_count, UsersCredit.count
# subscription assertions
assert_nil @user_without_subscription.subscribed_plan
# reservation assertions
reservation = Reservation.last
@ -286,7 +308,7 @@ module Reservations
assert_equal transaction.id, invoice.wallet_transaction_id
end
test 'user reserves a machine and plan pay by wallet with success' do
test 'user reserves a machine and a subscription pay by wallet with success' do
@vlonchamp = User.find_by(username: 'vlonchamp')
machine = Machine.find(6)
availability = machine.availabilities.first
@ -320,6 +342,10 @@ module Reservations
assert_equal users_credit_count + 1, UsersCredit.count
assert_equal wallet_transactions_count + 1, WalletTransaction.count
# subscription assertions
assert_not_nil @vlonchamp.subscribed_plan
assert_equal plan.id, @vlonchamp.subscribed_plan.id
# reservation assertions
reservation = Reservation.last
@ -351,7 +377,7 @@ module Reservations
assert_equal transaction.id, invoice.wallet_transaction_id
end
test "user without subscription and with invoicing disabled reserves a machine and pay wallet with success" do
test 'user without subscription and with invoicing disabled reserves a machine and pay wallet with success' do
@vlonchamp = User.find_by(username: 'vlonchamp')
@vlonchamp.update!(invoicing_disabled: true)
machine = Machine.find(6)
@ -363,16 +389,17 @@ module Reservations
users_credit_count = UsersCredit.count
post reservations_path, { reservation: {
user_id: @vlonchamp.id,
reservable_id: machine.id,
reservable_type: machine.class.name,
slots_attributes: [
{ start_at: availability.start_at.to_s(:iso8601),
end_at: (availability.start_at + 1.hour).to_s(:iso8601),
availability_id: availability.id
}
]
}}.to_json, default_headers
user_id: @vlonchamp.id,
reservable_id: machine.id,
reservable_type: machine.class.name,
slots_attributes: [
{
start_at: availability.start_at.to_s(:iso8601),
end_at: (availability.start_at + 1.hour).to_s(:iso8601),
availability_id: availability.id
}
]
} }.to_json, default_headers
# general assertions
assert_equal 201, response.status
@ -381,6 +408,9 @@ module Reservations
assert_equal invoice_items_count, InvoiceItem.count
assert_equal users_credit_count, UsersCredit.count
# subscription assertions
assert_nil @user_without_subscription.subscribed_plan
# reservation assertions
reservation = Reservation.last
@ -390,5 +420,79 @@ module Reservations
# notification
assert_not_empty Notification.where(attached_object: reservation)
end
test 'user reserves a training and a subscription with success' do
training = Training.first
availability = training.availabilities.first
plan = Plan.where(group_id: @user_without_subscription.group.id, type: 'Plan').first
reservations_count = Reservation.count
invoice_count = Invoice.count
invoice_items_count = InvoiceItem.count
users_credit_count = UsersCredit.count
post reservations_path, { reservation: {
user_id: @user_without_subscription.id,
plan_id: plan.id,
reservable_id: training.id,
reservable_type: training.class.name,
slots_attributes: [
{
start_at: availability.start_at.to_s(:iso8601),
end_at: (availability.start_at + 1.hour).to_s(:iso8601),
offered: false,
availability_id: availability.id
}
]
} }.to_json, default_headers
# general assertions
assert_equal 201, response.status
assert_equal Mime::JSON, response.content_type
result = json_response(response.body)
# Check the DB objects have been created as they should
assert_equal reservations_count + 1, Reservation.count
assert_equal invoice_count + 1, Invoice.count
assert_equal invoice_items_count + 2, InvoiceItem.count
assert_equal users_credit_count + 1, UsersCredit.count
# subscription assertions
assert_not_nil @user_without_subscription.subscribed_plan
assert_equal plan.id, @user_without_subscription.subscribed_plan.id
# reservation assertions
reservation = Reservation.find(result[:id])
assert reservation.invoice
assert reservation.stp_invoice_id.blank?
assert_equal 2, reservation.invoice.invoice_items.count
# credits assertions
assert_equal 1, @user_without_subscription.credits.count
assert_equal 'Training', @user_without_subscription.credits.last.creditable_type
assert_equal training.id, @user_without_subscription.credits.last.creditable_id
# invoice assertions
invoice = reservation.invoice
assert invoice.stp_invoice_id.blank?
refute invoice.total.blank?
assert_equal plan.amount, invoice.total
# invoice_items
invoice_items = InvoiceItem.last(2)
assert(invoice_items.all? { |ii| ii.stp_invoice_item_id.blank? })
assert(invoice_items.any? { |ii| ii.amount == plan.amount && !ii.subscription_id.nil? })
assert(invoice_items.any? { |ii| ii.amount.zero? })
# invoice assertions
invoice = Invoice.find_by(invoiced: reservation)
assert_invoice_pdf invoice
# notification
assert_not_empty Notification.where(attached_object: reservation)
end
end
end

View File

@ -19,17 +19,18 @@ module Reservations
VCR.use_cassette('reservations_create_for_machine_without_subscription_success') do
post reservations_path, { reservation: {
user_id: @user_without_subscription.id,
reservable_id: machine.id,
reservable_type: machine.class.name,
card_token: stripe_card_token,
slots_attributes: [
{ start_at: availability.start_at.to_s(:iso8601),
end_at: (availability.start_at + 1.hour).to_s(:iso8601),
availability_id: availability.id
}
]
}}.to_json, default_headers
user_id: @user_without_subscription.id,
reservable_id: machine.id,
reservable_type: machine.class.name,
card_token: stripe_card_token,
slots_attributes: [
{
start_at: availability.start_at.to_s(:iso8601),
end_at: (availability.start_at + 1.hour).to_s(:iso8601),
availability_id: availability.id
}
]
} }.to_json, default_headers
end
# general assertions
@ -40,6 +41,9 @@ module Reservations
assert_equal users_credit_count, UsersCredit.count
assert_equal subscriptions_count, Subscription.count
# subscription assertions
assert_nil @user_without_subscription.subscribed_plan
# reservation assertions
reservation = Reservation.last
@ -80,17 +84,18 @@ module Reservations
VCR.use_cassette('reservations_create_for_machine_without_subscription_error') do
post reservations_path, { reservation: {
user_id: @user_without_subscription.id,
reservable_id: machine.id,
reservable_type: machine.class.name,
card_token: stripe_card_token(error: :card_declined),
slots_attributes: [
{ start_at: availability.start_at.to_s(:iso8601),
end_at: (availability.start_at + 1.hour).to_s(:iso8601),
availability_id: availability.id
}
]
}}.to_json, default_headers
user_id: @user_without_subscription.id,
reservable_id: machine.id,
reservable_type: machine.class.name,
card_token: stripe_card_token(error: :card_declined),
slots_attributes: [
{
start_at: availability.start_at.to_s(:iso8601),
end_at: (availability.start_at + 1.hour).to_s(:iso8601),
availability_id: availability.id
}
]
} }.to_json, default_headers
end
# general assertions
@ -113,18 +118,18 @@ module Reservations
VCR.use_cassette('reservations_create_for_training_without_subscription_success') do
post reservations_path, { reservation: {
user_id: @user_without_subscription.id,
reservable_id: training.id,
reservable_type: training.class.name,
card_token: stripe_card_token,
slots_attributes: [
{
start_at: availability.start_at.to_s(:iso8601),
end_at: availability.end_at.to_s(:iso8601),
availability_id: availability.id
}
]
}}.to_json, default_headers
user_id: @user_without_subscription.id,
reservable_id: training.id,
reservable_type: training.class.name,
card_token: stripe_card_token,
slots_attributes: [
{
start_at: availability.start_at.to_s(:iso8601),
end_at: availability.end_at.to_s(:iso8601),
availability_id: availability.id
}
]
} }.to_json, default_headers
end
# general assertions
@ -173,21 +178,23 @@ module Reservations
VCR.use_cassette('reservations_create_for_machine_with_subscription_success') do
post reservations_path, { reservation: {
user_id: @user_with_subscription.id,
reservable_id: machine.id,
reservable_type: machine.class.name,
card_token: stripe_card_token,
slots_attributes: [
{ start_at: availability.start_at.to_s(:iso8601),
end_at: (availability.start_at + 1.hour).to_s(:iso8601),
availability_id: availability.id
},
{ start_at: (availability.start_at + 1.hour).to_s(:iso8601),
end_at: (availability.start_at + 2.hours).to_s(:iso8601),
availability_id: availability.id
}
]
}}.to_json, default_headers
user_id: @user_with_subscription.id,
reservable_id: machine.id,
reservable_type: machine.class.name,
card_token: stripe_card_token,
slots_attributes: [
{
start_at: availability.start_at.to_s(:iso8601),
end_at: (availability.start_at + 1.hour).to_s(:iso8601),
availability_id: availability.id
},
{
start_at: (availability.start_at + 1.hour).to_s(:iso8601),
end_at: (availability.start_at + 2.hours).to_s(:iso8601),
availability_id: availability.id
}
]
} }.to_json, default_headers
end
# general assertions
@ -214,9 +221,9 @@ module Reservations
invoice_items = InvoiceItem.last(2)
machine_price = machine.prices.find_by(group_id: @user_with_subscription.group_id, plan_id: plan.id).amount
assert invoice_items.any? { |invoice| invoice.amount == 0 }
assert invoice_items.any? { |invoice| invoice.amount == machine_price }
assert invoice_items.all? { |invoice| invoice.stp_invoice_item_id }
assert(invoice_items.any? { |inv| inv.amount.zero? })
assert(invoice_items.any? { |inv| inv.amount == machine_price })
assert(invoice_items.all?(&:stp_invoice_item_id))
# users_credits assertions
users_credit = UsersCredit.last
@ -246,18 +253,18 @@ module Reservations
VCR.use_cassette('reservations_create_for_training_with_subscription_success') do
post reservations_path, { reservation: {
user_id: @user_with_subscription.id,
reservable_id: training.id,
reservable_type: training.class.name,
card_token: stripe_card_token,
slots_attributes: [
{
start_at: availability.start_at.to_s(:iso8601),
end_at: availability.end_at.to_s(:iso8601),
availability_id: availability.id
}
]
}}.to_json, default_headers
user_id: @user_with_subscription.id,
reservable_id: training.id,
reservable_type: training.class.name,
card_token: stripe_card_token,
slots_attributes: [
{
start_at: availability.start_at.to_s(:iso8601),
end_at: availability.end_at.to_s(:iso8601),
availability_id: availability.id
}
]
} }.to_json, default_headers
end
# general assertions
@ -310,17 +317,18 @@ module Reservations
VCR.use_cassette('reservations_create_for_machine_and_pay_wallet_success') do
post reservations_path, { reservation: {
user_id: @vlonchamp.id,
reservable_id: machine.id,
reservable_type: machine.class.name,
card_token: stripe_card_token,
slots_attributes: [
{ start_at: availability.start_at.to_s(:iso8601),
end_at: (availability.start_at + 1.hour).to_s(:iso8601),
availability_id: availability.id
}
]
}}.to_json, default_headers
user_id: @vlonchamp.id,
reservable_id: machine.id,
reservable_type: machine.class.name,
card_token: stripe_card_token,
slots_attributes: [
{
start_at: availability.start_at.to_s(:iso8601),
end_at: (availability.start_at + 1.hour).to_s(:iso8601),
availability_id: availability.id
}
]
} }.to_json, default_headers
end
# general assertions
@ -381,19 +389,19 @@ module Reservations
VCR.use_cassette('reservations_create_for_training_and_plan_by_pay_wallet_success') do
post reservations_path, { reservation: {
user_id: @user_without_subscription.id,
reservable_id: training.id,
reservable_type: training.class.name,
card_token: stripe_card_token,
plan_id: plan.id,
slots_attributes: [
{
start_at: availability.start_at.to_s(:iso8601),
end_at: availability.end_at.to_s(:iso8601),
availability_id: availability.id
}
]
}}.to_json, default_headers
user_id: @user_without_subscription.id,
reservable_id: training.id,
reservable_type: training.class.name,
card_token: stripe_card_token,
plan_id: plan.id,
slots_attributes: [
{
start_at: availability.start_at.to_s(:iso8601),
end_at: availability.end_at.to_s(:iso8601),
availability_id: availability.id
}
]
} }.to_json, default_headers
end
# general assertions
@ -454,10 +462,11 @@ module Reservations
reservable_type: machine.class.name,
card_token: stripe_card_token,
slots_attributes: [
{ start_at: availability.start_at.to_s(:iso8601),
end_at: (availability.start_at + 1.hour).to_s(:iso8601),
availability_id: availability.id
}
{
start_at: availability.start_at.to_s(:iso8601),
end_at: (availability.start_at + 1.hour).to_s(:iso8601),
availability_id: availability.id
}
],
plan_id: plan.id
},
@ -531,19 +540,20 @@ module Reservations
VCR.use_cassette('reservations_training_with_expired_coupon_error') do
post reservations_path, {
reservation: {
user_id: @user_without_subscription.id,
reservable_id: training.id,
reservable_type: training.class.name,
card_token: stripe_card_token,
slots_attributes: [
{ start_at: availability.start_at.to_s(:iso8601),
end_at: (availability.start_at + 1.hour).to_s(:iso8601),
availability_id: availability.id
}
],
},
coupon_code: 'XMAS10'
reservation: {
user_id: @user_without_subscription.id,
reservable_id: training.id,
reservable_type: training.class.name,
card_token: stripe_card_token,
slots_attributes: [
{
start_at: availability.start_at.to_s(:iso8601),
end_at: (availability.start_at + 1.hour).to_s(:iso8601),
availability_id: availability.id
}
]
},
coupon_code: 'XMAS10'
}.to_json, default_headers
end

View File

@ -2,14 +2,23 @@ require 'test_helper'
class ExportTest < ActiveSupport::TestCase
test 'export must have a category' do
e = Export.new({export_type: 'global', user: User.first, query: '{"query":{"bool":{"must":[{"range":{"date":{"gte":"2016-06-25T02:00:00+02:00","lte":"2016-07-25T23:59:59+02:00"}}}]}}}'})
e = Export.new(
export_type: 'global',
user: User.first,
query: '{"query":{"bool":{"must":[{"range":{"date":{"gte":"2016-06-25T02:00:00+02:00","lte":"2016-07-25T23:59:59+02:00"}}}]}}}'
)
assert e.invalid?
end
test 'export generate an XLSX file' do
e = Export.create({category: 'statistics', export_type: 'global', user: User.first, query: '{"query":{"bool":{"must":[{"range":{"date":{"gte":"2016-06-25T02:00:00+02:00","lte":"2016-07-25T23:59:59+02:00"}}}]}}}'})
e = Export.create(
category: 'statistics',
export_type: 'global',
user: User.first,
query: '{"query":{"bool":{"must":[{"range":{"date":{"gte":"2016-06-25T02:00:00+02:00","lte":"2016-07-25T23:59:59+02:00"}}}]}}}'
)
e.save!
VCR.use_cassette("export_generate_an_xlsx_file") do
VCR.use_cassette('export_generate_an_xlsx_file', match_requests_on: [:query]) do
assert_export_xlsx e
end
end

View File

@ -2,9 +2,9 @@ require 'test_helper'
class SpaceTest < ActiveSupport::TestCase
bio_lab = {
name: 'Bio-lab',
description: 'An biological laboratory to experiment bio-technologies',
default_places: 5
name: 'Bio-lab',
description: 'An biological laboratory to experiment bio-technologies',
default_places: 5
}
test 'create a space' do
@ -17,7 +17,7 @@ class SpaceTest < ActiveSupport::TestCase
test 'update a space' do
new_name = 'Bio-tech lab'
space = Space.create!(bio_lab)
space.update_attributes({name: new_name})
space.update_attributes(name: new_name)
subtype = StatisticSubType.find_by(key: space.slug)
assert_equal new_name, subtype.label
end