Skip to content

Commit

Permalink
implement get casa cases endpoint + specs
Browse files Browse the repository at this point in the history
  • Loading branch information
xihai01 committed Aug 8, 2023
1 parent 3ca2d30 commit ecdfcb1
Show file tree
Hide file tree
Showing 6 changed files with 75 additions and 1 deletion.
1 change: 1 addition & 0 deletions app/controllers/api/v1/base_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ class Api::V1::BaseController < ActionController::API
def authenticate_user!
token, options = ActionController::HttpAuthentication::Token.token_and_options(request)
# return nil unless token && options.is_a?(Hash)
print options
user = User.find_by(email: options[:email])
p "#######"
print token
Expand Down
10 changes: 10 additions & 0 deletions app/controllers/api/v1/casa_cases_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
class Api::V1::CasaCasesController < Api::V1::BaseController
before_action :set_casa_case, only: [:show, :update, :destroy]

# GET /api/v1/casa_cases
def index
@casa_cases = current_user.casa_cases.all
# puts current_user.inspect
render json: @casa_cases, each_serializer: Api::V1::CasaCaseSerializer, status: :ok
end
end
16 changes: 16 additions & 0 deletions app/serializers/api/v1/casa_case_serializer.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
class Api::V1::CasaCaseSerializer < ActiveModel::Serializer
type "casa_case"
attributes :id, :case_number, :casa_org_id, :birthday, :court_report_status, :court_report_submit_date, :youth_date_in_care

def youth_date_in_care
object.date_in_care.strftime("%Y-%m-%d") if object.date_in_care.present?
end

def birthday
object.birth_month_year_youth.strftime("%Y-%m-%d") if object.birth_month_year_youth.present?
end

def court_report_submit_date
object.court_report_submitted_at.strftime("%Y-%m-%d") if object.court_report_submitted_at.present?
end
end
2 changes: 1 addition & 1 deletion config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -193,8 +193,8 @@
namespace :v1 do
namespace :users do
post "sign_in", to: "sessions#create"
# get 'sign_out', to: 'sessions#destroy'
end
get "casa_cases", to: "casa_cases#index"
end
end
end
27 changes: 27 additions & 0 deletions spec/controllers/api/v1/casa_cases_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
require "rails_helper"
require "spec_helper"

RSpec.describe Api::V1::CasaCasesController, type: :api do
describe "as a volunteer" do
describe "GET /api/v1/casa_cases" do
let(:casa_org) { create(:casa_org) }
let(:volunteer) { create(:volunteer, casa_org: casa_org) }

it "should return a list of volunteer's assigned casa cases" do
casa_cases = create_list(:casa_case, 5, casa_org: casa_org)
create_list(:casa_case, 2, casa_org: casa_org)
volunteer.casa_cases << casa_cases
puts volunteer.casa_cases.inspect
header("Authorization", "Token token=#{volunteer.token}, email=#{volunteer.email}")
get "/api/v1/casa_cases"
expect(last_response.status).to eq 200
expect(last_response.content_type).to eq("application/json; charset=utf-8")

body = JSON.parse(last_response.body, symbolize_names: true)
puts body.inspect
expect(body.length).to eq 5
expect(body).to match_array casa_cases.map { |casa_case| Api::V1::CasaCaseSerializer.new(casa_case).as_json }
end
end
end
end
20 changes: 20 additions & 0 deletions spec/serializers/api/v1/casa_case_serializer_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
require "rails_helper"

RSpec.describe Api::V1::CasaCaseSerializer, type: :serializer do
before(:each) do
@casa_case = create(:casa_case)
@serializer = Api::V1::CasaCaseSerializer.new(@casa_case)
@serialization = ActiveModelSerializers::Adapter.create(@serializer)
end

subject { JSON.parse(@serialization.to_json) }

it "should have matching attributes" do
# match some attributes returned by serializer
expect(subject["id"]).to eq(@casa_case.id)
expect(subject["case_number"]).to eq(@casa_case.case_number)
expect(subject["birthday"]).to eq(@casa_case.birth_month_year_youth.strftime("%Y-%m-%d"))
expect(subject["casa_org_id"]).to eq(@casa_case.casa_org_id)
expect(subject.length).to eq(7)
end
end

0 comments on commit ecdfcb1

Please sign in to comment.