Skip to content

Commit

Permalink
Merge pull request #122 from npauzenga/tasks/game-night-acceptance-specs
Browse files Browse the repository at this point in the history
complete game_night acceptance specs
  • Loading branch information
npauzenga committed Jan 31, 2016
2 parents 1e4e279 + b161d2f commit 24859f6
Show file tree
Hide file tree
Showing 7 changed files with 284 additions and 2 deletions.
15 changes: 15 additions & 0 deletions app/controllers/game_nights_controller.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
class GameNightsController < AuthenticationController
# POST /game_nights
def create
result = CreateGameNight.call(user: current_user,
game_night_params: game_night_params)
Expand All @@ -10,6 +11,7 @@ def create
end
end

# PATCH /game_nights
def update
result = UpdateGameNight.call(id: params[:id],
game_night_params: game_night_params)
Expand All @@ -21,6 +23,7 @@ def update
end
end

# GET /game_nights/:id
def show
result = ShowGameNight.call(id: params[:id])

Expand All @@ -31,6 +34,18 @@ def show
end
end

# GET /game_nights
def index
result = IndexGameNight.call

if result.success?
render json: result.game_nights, status: :ok
else
render json: result.errors, status: :internal_server_error
end
end

# DELETE /game_nights/:id
def destroy
result = DeleteGameNight.call(id: params[:id])

Expand Down
9 changes: 9 additions & 0 deletions app/interactors/index_game_night.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
class IndexGameNight < StandardInteraction
def execute
context.game_nights = GameNight.all
end

def validate_output
context.fail!(errors: "internal server error") unless context.game_nights
end
end
2 changes: 1 addition & 1 deletion app/interactors/update_game_night.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ def execute
private

def update_game_night
GameNight.update(context.id, context.game_night_params)
context.game_night = GameNight.update(context.id, context.game_night_params)
end

def valid_input?
Expand Down
2 changes: 1 addition & 1 deletion app/serializers/game_night_serializer.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
class GameNightSerializer < ActiveModel::Serializer
attributes :id, :time, :location_name, :location_address
attributes :id, :name, :time, :location_name, :location_address
end
142 changes: 142 additions & 0 deletions spec/acceptance/game_nights_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
require "rspec_api_documentation/dsl"

RSpec.resource "GameNights" do
let(:authenticated_user) do
create(:confirmed_user)
end

let(:auth_token) do
Knock::AuthToken.new(payload: { sub: authenticated_user.id }).token
end

def schema_path
"./config/schema/schemata/game_night.json"
end

shared_context "game_night parameters" do
parameter "name", <<-DESC, scope: :game_night
The name of the game_night.
DESC
parameter "time", <<-DESC, scope: :game_night, required: true
The time of the game_night.
DESC
parameter "location_name", <<-DESC, scope: :game_night, required: true
The location of the game_night.
DESC
parameter "location_address", <<-DESC, scope: :game_night
The address of the game_night.
DESC
end

post "/game_nights" do
let(:schema_path) { "#{Rails.root}/config/schema/api.json" }
let(:last_response) { response }
let(:last_request) { request }

include_context "game_night parameters"

let(:name) { "Waaggh" }
let(:time) { "2015-09-12 22:49:27 +0530" }
let(:location_name) { "Library" }
let(:location_address) { "999 main st. Silver Spring, MD 20910" }

header "Authorization", :auth_token

example_request "POST /game_nights" do
expect(status).to eq 201

json_response = JSON.parse(response_body)

expect(json_response["data"]["attributes"]["name"]).
to eq public_send(:name)

expect(authenticated_user.game_nights.first.name).
to eq public_send(:name)

game_night = GameNight.first

expect(game_night.users.first).to eq authenticated_user
end
end

patch "/game_nights/:id" do
include_context "game_night parameters"

let!(:game_night) { create(:game_night) }
let(:id) { game_night.id }

parameter "id", <<-DESC, required: true
The id of the game_night.
DESC

let(:name) { "New Name" }
let(:time) { "2015-09-12 22:49:27 +0530" }
let(:location_name) { "Library" }
let(:location_address) { "999 main st. Silver Spring, MD 20910" }

header "Authorization", :auth_token

example_request "PATCH /game_nights/:id" do
expect(status).to eq 200

game_night = JSON.parse(response_body)

expect(game_night["data"]["attributes"]["name"]).
to eq public_send(:name)
end
end

get "/game_nights/:id" do
let!(:game_night) { create(:game_night) }
let(:id) { game_night.id }
let(:name) { game_night.name }

parameter "id", <<-DESC, required: true
The id of the game_night.
DESC

header "Authorization", :auth_token

example_request "GET /game_nights/:id" do
expect(status).to eq 200

game_night = JSON.parse(response_body)

expect(game_night["data"]["attributes"]["name"]).
to eq public_send(:name)
end
end

get "/game_nights/" do
let!(:game_night1) { create(:game_night) }
let!(:game_night2) { create(:game_night) }
let!(:game_night3) { create(:game_night) }

header "Authorization", :auth_token

example_request "GET /game_nights/" do
expect(status).to eq 200

list = JSON.parse(response_body)

expect(list["data"][0].value?(game_night1.id.to_s)).to be_truthy
expect(list["data"][1].value?(game_night2.id.to_s)).to be_truthy
expect(list["data"][2].value?(game_night3.id.to_s)).to be_truthy
end
end

delete "/game_nights/:id" do
let!(:game_night) { create(:game_night) }
let(:id) { game_night.id }

header "Authorization", :auth_token

parameter "id", <<-DESC, required: true
The id of the game_night.
DESC

example_request "DELETE /game_nights/:id" do
expect(status).to eq 204
end
end
end
80 changes: 80 additions & 0 deletions spec/controllers/game_nights_controller_spec.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
include Committee::Test::Methods

RSpec.describe GameNightsController do
let(:user) { create(:confirmed_user) }
let(:game_night) { create(:game_night) }
let(:schema_path) { "#{Rails.root}/config/schema/api.json" }
let(:last_response) { response }
let(:last_request) { request }

before do
authenticate
Expand Down Expand Up @@ -48,6 +53,11 @@
post :create, params
expect(serialize(game_night)).to eq(response.body)
end

it "conforms to JSON schema" do
post :create, params
assert_schema_conform
end
end

context "when CreateGameNight is a failure" do
Expand Down Expand Up @@ -111,6 +121,11 @@
patch :update, params
expect(serialize(game_night)).to eq(response.body)
end

it "conforms to JSON schema" do
patch :update, params
assert_schema_conform
end
end

context "when UpdateGameNight is a failure" do
Expand Down Expand Up @@ -161,6 +176,11 @@
get :show, params
expect(serialize(game_night)).to eq(response.body)
end

it "conforms to JSON schema" do
get :show, params
assert_schema_conform
end
end

context "when ShowGameNight is a failure" do
Expand Down Expand Up @@ -227,4 +247,64 @@
end
end
end

describe "GET #index" do
let(:game_night1) { create(:game_night) }
let(:game_night2) { create(:game_night) }
let(:game_night3) { create(:game_night) }

let(:list) { [game_night1, game_night2, game_night3] }

let(:index_profile_context) do
Interactor::Context.new(errors: :val, game_nights: list)
end

before(:example) do
allow(IndexGameNight).to receive(:call).and_return(index_profile_context)
end

before do
authenticate
end

context "when succesful" do
it "calls the IndexProfile interactor" do
expect(IndexGameNight).to receive(:call)
get :index
end

it "returns HTTP status 200" do
get :index
expect(response).to have_http_status(200)
end

it "render the profiles as JSON" do
get :index
expect(json["data"][0].value?(game_night1.id.to_s)).to be_truthy
expect(json["data"][1].value?(game_night2.id.to_s)).to be_truthy
expect(json["data"][2].value?(game_night3.id.to_s)).to be_truthy
end

it "conforms to JSON schema" do
get :index
assert_schema_conform
end
end

context "when IndexProfile fails" do
let(:index_profile_context) do
double(:context, errors: "internal server error", success?: false)
end

it "returns HTTP status 500" do
get :index
expect(response).to have_http_status(500)
end

it "renders an error" do
get :index
expect(response.body).to eq("internal server error")
end
end
end
end
36 changes: 36 additions & 0 deletions spec/interactors/index_game_night_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
RSpec.describe IndexGameNight do
describe ".call" do
subject do
described_class.call
end

let!(:game_night1) { create(:game_night) }
let!(:game_night2) { create(:game_night) }
let!(:game_night3) { create(:game_night) }

context "when successful" do
it "returns a successful context" do
is_expected.to be_a_success
end

it "sets game_nights to an array of game_nights" do
expect(subject.game_nights).
to eq([game_night1, game_night2, game_night3])
end
end

context "when unsuccessful" do
before do
allow(GameNight).to receive(:all).and_return(nil)
end

it "fails" do
is_expected.to be_a_failure
end

it "returns an error" do
expect(subject.errors).to eq("internal server error")
end
end
end
end

0 comments on commit 24859f6

Please sign in to comment.