Skip to content

Commit

Permalink
Merge branch 'devel' into issue_41_refactoring_tags
Browse files Browse the repository at this point in the history
  • Loading branch information
vitorbaraujo committed Nov 25, 2016
2 parents cd1502a + cbdb8af commit a4ee0a2
Show file tree
Hide file tree
Showing 5 changed files with 173 additions and 25 deletions.
18 changes: 11 additions & 7 deletions app/controllers/rooms_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ class RoomsController < ApplicationController

skip_before_action :verify_authenticity_token if Rails.env.test?
before_action :authenticate_member
before_action :is_owner, only: [:ban_member, :reintegrate_member]
before_action :is_owner, only: [:ban_member]

def index
@rooms = Room.all
Expand Down Expand Up @@ -93,7 +93,7 @@ def destroy
end

def ban_member
member = Member.find(params[:member_id])
member = Member.find(params[:member_id])
unless params[:topic_id].nil?
topic = Topic.find(params[:topic_id])
room = topic.room
Expand Down Expand Up @@ -135,11 +135,15 @@ def members_list

def reintegrate_member
@room = Room.find(params[:id])
@room.black_list.delete(params[:member_id].to_i)
@room.save

flash[:notice] = "Member has been removed from black list and can now join the room"
redirect_to banned_members_url
if @room.owner == current_member
@room.black_list.delete(params[:member_id].to_i)
@room.save
flash[:notice] = "Member has been removed from black list and can now join the room"
redirect_to banned_members_url(@room)
else
flash[:notice] = "You do not have permission to do this action"
redirect_to room_path(@room)
end
end

private
Expand Down
2 changes: 1 addition & 1 deletion app/views/rooms/show.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@
</h1>
<div id="room-buttons">
<% if current_member == @room.owner %>
<%= link_to 'New Topic', new_room_topic_path(@room), class: 'btn btn-sm btn-success', id: "new-topic-button"%>
<%= link_to 'Edit', edit_room_path(@room), class: 'btn btn-sm btn-primary'%>
<%= link_to 'Members List', members_list_url, class: 'btn btn-sm btn-default'%>
<%= link_to 'Banned Members', banned_members_url, class: 'btn btn-sm btn-default'%>
<%= link_to 'New Topic', new_room_topic_path(@room), class: 'btn btn-sm btn-default', id: "new-topic-button"%>
<% elsif @room.presence_in current_member.rooms %>
<%= link_to 'Exit', rooms_signout_path(id: @room.id), class: 'btn btn-sm btn-danger', method: :post %>
<% else %>
Expand Down
16 changes: 4 additions & 12 deletions test/controllers/question_controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,12 @@ class QuestionControllerTest < ActionDispatch::IntegrationTest
def setup
@false_option_anonymous = 0
@true_option_anonymous = 1
@member = Member.create(name: "Thalisson", alias: "thalisson", email: "thalisson@gmail.com", password: "12345678", password_confirmation: "12345678")

@room = Room.new(name: "calculo 1", description: "teste1")
@room.owner = @member
@room.save

@topic = @room.topics.new(name: "limites", description: "description1")
@topic.save

attachment = fixture_file_upload('test/fixtures/sample_files/file.png', 'image/png')

@question = @topic.questions.new(content: "How did I get here?", attachment: attachment)
@question.member = @member
@question.save
@member = Member.create(name: "Thalisson", alias: "thalisson", email: "thalisson@gmail.com", password: "12345678", password_confirmation: "12345678")
@room = Room.create(name: "calculo 1", description: "teste1", owner: @member)
@topic = @room.topics.create(name: "limites", description: "description1")
@question = @topic.questions.create(content: "How did I get here?", member: @member, attachment: attachment)

sign_in_as @member
end
Expand Down
73 changes: 68 additions & 5 deletions test/controllers/room_controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -95,13 +95,13 @@ def setup
end

test "should user be in room's black_list if owner bans him" do
@another_member.rooms << @room
@another_member.save
@another_member.rooms << @room
@another_member.save

post "/topics/#{@topic.id}/ban_member", params: {member_id: @another_member.id, topic_id: @topic.id}
@room.reload
post "/topics/#{@topic.id}/ban_member", params: {member_id: @another_member.id, topic_id: @topic.id}
@room.reload

assert @room.black_list.include? @another_member.id
assert @room.black_list.include? @another_member.id
end

test "should user not be banned by another regular user" do
Expand Down Expand Up @@ -238,4 +238,67 @@ def setup
assert_redirected_to members_list_path(@room)
end

test "should room owner get banned members page" do
get "/rooms/#{@room.id}/banned_members"

assert_response :success
end

test "should regular member not be able to access banned members page" do
sign_out_as @member
sign_in_as @another_member

get "/rooms/#{@room.id}/banned_members"

assert_redirected_to room_path(@room)
assert_equal "You do not have permission to do this action", flash[:notice]
end

test "should user cannot be banned twice" do
@another_member.rooms << @room
@another_member.save

post "/topics/#{@topic.id}/ban_member", params: { member_id: @another_member.id, topic_id: @topic.id }
@room.reload

assert @room.black_list.include? @another_member.id

# trying to ban member twice
post "/topics/#{@topic.id}/ban_member", params: { member_id: @another_member.id, topic_id: @topic.id }

assert "The member is already banned", flash[:notice]
end

test "should reintegrate member from blacklist" do
@another_member.rooms << @room
@another_member.save

@room.black_list << @another_member.id
@room.save

post "/rooms/#{@room.id}/reintegrate_member", params: { member_id: @another_member.id }

@room.reload

assert_redirected_to banned_members_url(@room)
assert_not @room.black_list.include? @another_member.id
assert_equal "Member has been removed from black list and can now join the room", flash[:notice]
end

test "should not reintegrate member from blacklist if not owner" do
@another_member.rooms << @room
@another_member.save

@room.black_list << @another_member.id
@room.save

sign_out_as @member
sign_in_as @another_member

post "/rooms/#{@room.id}/reintegrate_member", params: { member_id: @another_member.id }

assert_redirected_to room_path(@room)
assert @room.black_list.include? @another_member.id
end

end
89 changes: 89 additions & 0 deletions test/helpers/notification_helper_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
require 'test_helper'
include SessionsHelper

class NotificationHelperTest < ActionView::TestCase

def setup
@member = Member.create(name: "Thalisson", alias: "thalisson", email: "thalisson@gmail.com", password: "12345678", password_confirmation: "12345678")
@another_member = Member.create(name: 'vitor', email: 'vitor@gmail.com', password: '123456', password_confirmation: '123456', alias: 'vitor')
@room = Room.create(name: "calculo 1", description: "teste1", owner: @member)
@topic = @room.topics.create(name: "limites", description: "description1")
@question = @topic.questions.create(content: "How did I get here?", member: @member)
@another_question = @topic.questions.create(content: "question?", member: @another_member)
@answer = @question.answers.create(content: "answer", member: @member)
@another_answer = @question.answers.create(content: "another answer", member: @another_member)
end

test "should send answered_question notification using answer" do
log_in @another_member

assert_difference('Notification.count', 1) do
send_notification('answered_question', @answer)
end
end

test "should send created_question notification" do
log_in @another_member

assert_difference('Notification.count', 1) do
send_notification('created_question', @question)
end
end

test "should send moderated_question notification" do
log_in @member

assert_difference('Notification.count', 1) do
send_notification('moderated_question', @another_question)
end
end

test "should send moderated_answer notification" do
log_in @member

assert_difference('Notification.count', 1) do
send_notification('moderated_answer', @another_answer)
end
end

test "should send reported_question notification" do
log_in @another_member

assert_difference('Notification.count', 1) do
send_notification('reported_question', @question)
end
end

test "should send reported_answer notification" do
log_in @another_member

assert_difference('Notification.count', 1) do
send_notification('reported_answer', @answer)
end
end

test "should send joined_room notification" do
log_in @another_member

assert_difference('Notification.count', 1) do
send_notification('joined_room', @room)
end
end

test "should send liked_question notification" do
log_in @another_member

assert_difference('Notification.count', 1) do
send_notification('liked_question', @question)
end
end

test "should send liked_answer notification" do
log_in @another_member

assert_difference('Notification.count', 1) do
send_notification('liked_answer', @answer)
end
end

end

0 comments on commit a4ee0a2

Please sign in to comment.