Skip to content
This repository has been archived by the owner on Dec 8, 2021. It is now read-only.

Commit

Permalink
Add unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
torresed committed Feb 18, 2021
1 parent a257ee5 commit 751971f
Show file tree
Hide file tree
Showing 4 changed files with 167 additions and 4 deletions.
6 changes: 3 additions & 3 deletions app/controllers/parties_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ def create

def update
@member = Friends.find_by(sent_by: @friend, sent_to: current_user, status: :confirmed)
if @member.nil? || !@member.invited? || @member.party_members.count >= 2
if @member.nil? || !@member.invited? || @friend.party_members.count >= 2
render status: :bad_request, json: { error: 'Invitation could not be accepted.' }
else
@member.accepted!
Expand All @@ -40,13 +40,13 @@ def kick

def destroy
current_user.party_members.destroy
current_user.invitations.destroy
current_user.invitations_pending.destroy
render status: :ok, json: { success: 'Party has been disbanded.' }
end

private

def members
def users
@user = current_user
head :bad_request unless (@friend = User.find_by(id: params[:id]))
end
Expand Down
132 changes: 132 additions & 0 deletions spec/requests/parties_controller_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
# frozen_string_literal: true

require 'rails_helper'

RSpec.describe PartiesController, type: :request do
context 'when logged out' do
let(:friend) { create(:user) }

describe 'GET #show' do
it 'renders an unauthorized response' do
get parties_url, as: :json
expect(response).to have_http_status(:unauthorized)
end
end

describe 'POST #create' do
it 'renders an unauthorized response' do
post parties_url, as: :json
expect(response).to have_http_status(:unauthorized)
end
end

describe 'POST #kick' do
it 'renders an unauthorized response' do
post parties_kick_url, params: { id: friend.id.to_s }, as: :json
expect(response).to have_http_status(:unauthorized)
end
end

describe 'PATCH #update' do
it 'renders an unauthorized response' do
patch parties_url, params: { id: friend.id.to_s }, as: :json
expect(response).to have_http_status(:unauthorized)
end
end

describe 'PUT #update' do
it 'renders an unauthorized response' do
put parties_url, params: { id: friend.id.to_s }, as: :json
expect(response).to have_http_status(:unauthorized)
end
end

describe 'DELETE #destroy' do
it 'renders an unauthorized response' do
delete parties_url, params: { id: friend.id.to_s }, as: :json
expect(response).to have_http_status(:unauthorized)
end
end
end

context 'when logged in' do
let(:user) { create(:user) }
let(:friend) { create(:user) }

let(:valid_headers) do
{ authorization: "Token #{user.auth_token}" }
end

let(:valid_headers_friend) do
{ authorization: "Token #{friend.auth_token}" }
end

describe 'GET #show' do
it 'gets party' do
get parties_url, headers: valid_headers, as: :json
expect(response).to have_http_status(:ok)
end
end

describe 'POST #create' do
it 'sends a party invitation' do
Friends.create(sent_by: user, sent_to: friend, status: :confirmed)
post parties_url, params: { id: friend.id.to_s }, headers: valid_headers, as: :json
expect(response.body).to match(a_string_including('Friend has been invited.'))
expect(user.invitations_pending).to include(friend)
end
end

it 'fails to send a self invitation' do
post parties_url, params: { id: user.id.to_s }, headers: valid_headers, as: :json
expect(response.body).to match(a_string_including('Friend could not be invited.'))
expect(user.invitations_pending).not_to include(user)
end

it 'fails to send an invite if there is one already pending' do
Friends.create(sent_by: user, sent_to: friend, status: :confirmed, invitation: :invited)
post parties_url, params: { id: friend.id.to_s }, headers: valid_headers, as: :json
expect(response.body).to match(a_string_including('Friend could not be invited.'))
end

describe 'PUT #update' do
it 'accepts a party invitation' do
Friends.create(sent_by: user, sent_to: friend, status: :confirmed, invitation: :invited)
put parties_url, params: { id: user.id.to_s }, headers: valid_headers_friend, as: :json
expect(response.body).to match(a_string_including('Invitation accepted.'))
expect(user.party_members).to include(friend)
end

it 'fails to accept a nonexistent invitation' do
Friends.create(sent_by: user, sent_to: friend, status: :confirmed, invitation: :unsolicited)
put parties_url, params: { id: friend.id.to_s }, headers: valid_headers, as: :json
expect(response.body).to match(a_string_including('Invitation could not be accepted.'))
expect(user.party_members).not_to include(friend)
end
end

describe 'DELETE #destroy' do
it 'disbands a party' do
Friends.create(sent_by: user, sent_to: friend, status: :confirmed, invitation: :accepted)
delete parties_url, params: { id: friend.id.to_s }, headers: valid_headers, as: :json
expect(response.body).to match(a_string_including('Party has been disbanded.'))
expect(user.friends_pending).not_to include(friend)
end
end

describe 'POST #kick' do
it 'kicks a party member' do
Friends.create(sent_by: user, sent_to: friend, status: :confirmed, invitation: :accepted)
post parties_kick_url, params: { id: friend.id.to_s }, headers: valid_headers, as: :json
expect(response.body).to match(a_string_including('Friend has been kicked.'))
expect(user.party_members).not_to include(friend)
end

it 'fails to kick a friend that is not in party' do
Friends.create(sent_by: user, sent_to: friend, status: :confirmed, invitation: :invited)
post parties_kick_url, params: { id: friend.id.to_s }, headers: valid_headers, as: :json
expect(response.body).to match(a_string_including('Friend could not be kicked.'))
end
end
end
end
2 changes: 1 addition & 1 deletion spec/routing/friends_routing_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
expect(patch: '/friends').to route_to('friends#update')
end

it 'routes to #destroy_session via DELETE' do
it 'routes to #destroy via DELETE' do
expect(delete: '/friends').to route_to('friends#destroy')
end
end
Expand Down
31 changes: 31 additions & 0 deletions spec/routing/parties_routing_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# frozen_string_literal: true

require 'rails_helper'

RSpec.describe PartiesController, type: :routing do
describe 'routing' do
it 'routes to #show via GET' do
expect(get: '/parties').to route_to('parties#show')
end

it 'routes to #create via POST' do
expect(post: '/parties').to route_to('parties#create')
end

it 'routes to #kick via POST' do
expect(post: '/parties/kick').to route_to('parties#kick')
end

it 'routes to #update via PUT' do
expect(put: '/parties').to route_to('parties#update')
end

it 'routes to #update via PATCH' do
expect(patch: '/parties').to route_to('parties#update')
end

it 'routes to #destroy via DELETE' do
expect(delete: '/parties').to route_to('parties#destroy')
end
end
end

0 comments on commit 751971f

Please sign in to comment.