Skip to content

Instantly share code, notes, and snippets.

@macouella
Last active December 18, 2017 20:52
Show Gist options
  • Save macouella/d7614913ab18a33acb7fba3644c97275 to your computer and use it in GitHub Desktop.
Save macouella/d7614913ab18a33acb7fba3644c97275 to your computer and use it in GitHub Desktop.
Show this to Jan because Ruby Factory Girl (js) === Python Factory Boy hahaha
# test/fixtures/abandoned_tickets.yml
<% two_days_ago = Time.now - 2.days %>
abandoned_ticket_one:
company: company_one
user: user_one
abandoned_ticket_two:
company: company_one
user: user_one
created_at: <%= two_days_ago %>
updated_at: <%= two_days_ago %>
# app/services/create_ticket_service.rb
class CreateTicketService
def initialize(ticket_params)
set_ticket_params(ticket_params)
end
def create
return { body: ticket_errors, valid: false } unless ticket.valid?
create_initial_ticket_action
remove_abandoned_tickets_within_last_day
body = ticket.attributes.merge({
fcm_notifications: send_fcm_notifications
})
{ body: body, valid: ticket.valid? }
end
private
def ticket_errors
ticket.errors
end
def send_fcm_notifications
ticket.send_fcm_notifications
end
def create_initial_ticket_action
ticket.create_initial_ticket_action
end
def remove_abandoned_tickets_within_last_day
client.abandoned_tickets.within_day.destroy_all
end
def ticket
@ticket ||= Ticket.create(@ticket_params)
end
def client
@client ||= ticket.creator
end
def set_ticket_params(ticket_params)
@ticket_params = ticket_params
end
end
# test/test_helper.rb
ENV['RAILS_ENV'] ||= 'test'
require File.expand_path('../../config/environment', __FILE__)
require 'rails/test_help'
require 'helpers/authentication_helper'
require 'helpers/file_helper'
class ActiveSupport::TestCase
# Setup all fixtures in test/fixtures/*.yml for all tests in alphabetical order.
fixtures :all
# Add more helper methods to be used by all tests here...
Minitest.after_run { `rm -rf public/uploads` } # Clean uploaded files from tests
end
class ActionDispatch::IntegrationTest
include AutheticationHelper
include FileHelper
end
# app/controllers/api/v1/tickets_controller.rb
module Api
module V1
class TicketsController < Api::BaseController
def index
render json: tickets, include: nested_associations
end
def show
render json: ticket, include: nested_associations
end
def create
response = CreateTicketService.new(create_ticket_params).create
if(response[:valid])
render json: response[:body], include: nested_associations, status: :created
else
render json: response[:body], status: :unprocessable_entity
end
ensure
close_photos
end
def update
if ticket.update(update_ticket_params)
render json: ticket, include: nested_associations, status: :ok
else
render json: ticket.errors, status: :unprocessable_entity
end
end
def destroy
ticket.destroy
render json: {}, status: :ok
end
private
def tickets
@tickets ||= current_user.can_access_employee_views? ?
current_user.company.tickets.latest_first : current_user.tickets.latest_first
end
def ticket
@ticket ||= tickets.find(params[:id])
end
def create_ticket_params
convert_base64_files
params.require(:ticket).
permit(:company_id, :service_id, :details, :is_urgent, photos: []).
merge!(creator_id: current_user.id)
end
def update_ticket_params
return params.require(:ticket).permit(:details) if current_user.client?
params.require(:ticket).
permit(:details, :is_urgent,ticket_actions_attributes: [
:ticket_status_id, :assignee_id, :notes, :internal_notes])
end
def nested_associations
'*.*'
end
def convert_base64_files
params[:ticket][:photos] = photos.map(&:params)
end
def photos
@photos ||= params[:ticket].fetch(:photos, []).map { |photo| Base64Param.new photo }
end
def close_photos
(@photos || []).each(&:close)
end
end
end
end
# test/controllers/api/v1/tickets_controller_test.rb
require 'test_helper'
class Api::V1::TicketsControllerTest < ActionDispatch::IntegrationTest
setup do
@abandoned_ticket_within_day = abandoned_tickets(:abandoned_ticket_one)
@abandoned_ticket_two_days_ago = abandoned_tickets(:abandoned_ticket_two)
@ticket = tickets(:ticket_one)
end
test "should use jwt" do
get api_v1_tickets_url
assert_response :unauthorized
end
test "should get index" do
get api_v1_tickets_url, headers: auth_header(users :user_one)
assert_response :success
assert JSON.parse(body).count > 0
end
test "should show ticket" do
get api_v1_ticket_url(@ticket), headers: auth_header(users :user_one)
assert_response :success
assert_equal @ticket.id, JSON.parse(body)['id']
end
test "should create ticket, send fcm notifications and delete abandoned tickets from the last day" do
assert_difference('Ticket.count') do
post api_v1_tickets_url, params: {
ticket: {
creator_id: users(:user_one).id,
company_id: companies(:company_one).id,
service_id: services(:service_one).id,
photos: [param_base64_image1, param_base64_image2],
details: 'details',
is_urgent: true
}
}, headers: auth_header(users :user_one)
end
post_response = response.parsed_body
fcm_responses = post_response['fcm_notifications']
creator_abandoned_tickets = @ticket.creator.abandoned_tickets
assert_response :created
assert creator_abandoned_tickets.within_day.find_by_id(@abandoned_ticket_within_day.id).nil?
assert creator_abandoned_tickets.any?
assert post_response['id'].present?
assert fcm_responses['employee']['response'] == "success"
assert fcm_responses['manager']['response'] == "success"
assert fcm_responses['client']['response'] == "success"
end
test "should create initial action" do
initial_ticket_action = @ticket.ticket_actions.first
assert initial_ticket_action.present?
assert initial_ticket_action.ticket_status.code == 'service_requested'
end
test "should update ticket" do
patch api_v1_ticket_url(@ticket), params: {
ticket: {
details: 'new_details',
is_urgent: true,
ticket_actions_attributes: {'0' => {
ticket_status_id: ticket_status(:ticket_status_one).id,
assignee_id: users(:user_one).id,
internal_notes: 'internal note',
notes: 'external note'
}}
}
}, headers: auth_header(users :user_one)
assert_response :success
assert_equal 'new_details', JSON.parse(body)['details']
end
test "should now allow client to update status" do
@ticket.update_column(:creator_id, users(:user_three).id)
n_action = @ticket.ticket_actions.count
patch api_v1_ticket_url(@ticket), params: {
ticket: {
details: 'new_details',
is_urgent: true,
ticket_actions_attributes: {'0' => {
ticket_status_id: ticket_status(:ticket_status_one).id,
assignee_id: users(:user_three).id,
internal_notes: 'internal note',
notes: 'external note'
}}
}
}, headers: auth_header(users :user_three)
assert_response :success
assert_equal 'new_details', JSON.parse(body)['details']
assert_equal n_action, JSON.parse(body)["ticket_actions"].count
end
test "should destroy ticket" do
assert_difference('Ticket.count', -1) do
delete api_v1_ticket_url(@ticket), headers: auth_header(users :user_one)
end
assert_response :success
end
end
@mojeto
Copy link

mojeto commented Dec 18, 2017

🤘

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment