Skip to content

Commit

Permalink
add vote controller test
Browse files Browse the repository at this point in the history
  • Loading branch information
jiangplus committed Sep 10, 2024
1 parent 489a283 commit c22455d
Show file tree
Hide file tree
Showing 7 changed files with 164 additions and 16 deletions.
5 changes: 3 additions & 2 deletions app/controllers/api/vote_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,8 @@ def cast_vote
raise AppError.new("voting time is ended") if DateTime.now > proposal.end_time
end

# TODO: multi choice
# TODO : multi choice
# TODO : eligibile group is only the parent group

weight = 1

Expand Down Expand Up @@ -99,7 +100,7 @@ def vote_proposal_params
params.require(:vote_proposal).permit(:title, :content, :show_voters, :max_choice,
:eligibile_group_id, :eligibile_badge_class_id, :eligibile_point_id,
:verification, :eligibility, :can_update_vote, :start_time, :end_time,
vote_options_attributes: [ :id, :title, :link, :_destroy ]
vote_options_attributes: [ :id, :title, :link, :content, :image_url, :_destroy ]
)
end
end
6 changes: 5 additions & 1 deletion app/models/vote_option.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
class VoteOption < ApplicationRecord
belongs_to :group
belongs_to :group, optional: true # remove group column
belongs_to :vote_proposal

before_save do
self.group_id ||= self.vote_proposal.group_id
end
end
17 changes: 17 additions & 0 deletions app/policies/vote_proposal_policy.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
class VoteProposalPolicy < ApplicationPolicy
attr_reader :profile, :proposal

def initialize(profile, proposal)
@profile = profile
@proposal = proposal
end

def update?
@proposal.creator_id == @profile.id
end

def cancel?
@proposal.creator_id == @profile.id && @proposal.vote_records.count == 0
end

end
7 changes: 6 additions & 1 deletion config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@

post "voucher/create", to: "voucher#create"
post "voucher/use", to: "voucher#use"
get "voucher/get_code", to: "voucher#get_code"
get "voucher/get_code", to: "voucher#get_code"
post "voucher/revoke", to: "voucher#revoke"
post "voucher/send_badge", to: "voucher#send_badge"
post "voucher/send_badge_by_address", to: "voucher#send_badge_by_address"
Expand All @@ -82,6 +82,11 @@
post "point/accept", to: "point#accept"
post "point/reject", to: "point#reject"
post "point/transfer", to: "point#transfer"

post "vote/create", to: "vote#create"
post "vote/update", to: "vote#update"
post "vote/cancel", to: "vote#cancel"
post "vote/cast_vote", to: "vote#cast_vote"
end

get "sign_in", to: "sessions#new"
Expand Down
18 changes: 9 additions & 9 deletions test/controllers/api/group_invite_controller_test.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
require "test_helper"

class Api::GroupInviteControllerTest < ActionDispatch::IntegrationTest
test "api/group/send_invite" do
test "api#group/send_invite" do
profile = Profile.find_by(handle: "cookie")
auth_token = profile.gen_auth_token
group = Group.find_by(handle: "guildx")
Expand All @@ -20,7 +20,7 @@ class Api::GroupInviteControllerTest < ActionDispatch::IntegrationTest
assert group_invite.status == "sending"
end

test "api/group/send_invite to existing member for upgrading" do
test "api#group/send_invite to existing member for upgrading" do
profile = Profile.find_by(handle: "cookie")
profile2 = Profile.find_by(handle: "mooncake")
auth_token = profile.gen_auth_token
Expand All @@ -41,7 +41,7 @@ class Api::GroupInviteControllerTest < ActionDispatch::IntegrationTest
assert Membership.find_by(profile_id: profile2.id, group_id: group.id).role == "manager"
end

test "api/group/send_invite to existing manager without downgrading" do
test "api#group/send_invite to existing manager without downgrading" do
profile = Profile.find_by(handle: "cookie")
profile2 = Profile.find_by(handle: "mooncake")
auth_token = profile.gen_auth_token
Expand All @@ -62,7 +62,7 @@ class Api::GroupInviteControllerTest < ActionDispatch::IntegrationTest
assert Membership.find_by(profile_id: profile2.id, group_id: group.id).role == "manager"
end

test "api/group/send_invite to new email and accept" do
test "api#group/send_invite to new email and accept" do
profile = Profile.find_by(handle: "cookie")
profile2 = Profile.find_by(handle: "dimsum")
auth_token = profile.gen_auth_token
Expand Down Expand Up @@ -94,7 +94,7 @@ class Api::GroupInviteControllerTest < ActionDispatch::IntegrationTest
assert_equal "manager", group_member.role
end

test "api/group/accept_invite" do
test "api#group/accept_invite" do
profile = Profile.find_by(handle: "mooncake")
auth_token = profile.gen_auth_token
group = Group.find_by(handle: "guildx")
Expand All @@ -111,7 +111,7 @@ class Api::GroupInviteControllerTest < ActionDispatch::IntegrationTest
assert_equal "member", group_member.role
end

test "api/group/cancel_invite" do
test "api#group/cancel_invite" do
profile = Profile.find_by(handle: "mooncake")
auth_token = profile.gen_auth_token
group = Group.find_by(handle: "guildx")
Expand All @@ -127,7 +127,7 @@ class Api::GroupInviteControllerTest < ActionDispatch::IntegrationTest
assert group_invite.status == "cancelled"
end

test "api/group/revoke_invite" do
test "api#group/revoke_invite" do
profile = Profile.find_by(handle: "cookie")
auth_token = profile.gen_auth_token
group = Group.find_by(handle: "guildx")
Expand All @@ -143,7 +143,7 @@ class Api::GroupInviteControllerTest < ActionDispatch::IntegrationTest
assert group_invite.status == "cancelled"
end

test "api/group/request_invite" do
test "api#group/request_invite" do
profile = Profile.find_by(handle: "mooncake")
auth_token = profile.gen_auth_token
group = Group.find_by(handle: "guildx")
Expand All @@ -163,7 +163,7 @@ class Api::GroupInviteControllerTest < ActionDispatch::IntegrationTest
assert_equal "I would like to join the group", group_invite.message
end

test "api/group/accept_request" do
test "api#group/accept_request" do
profile = Profile.find_by(handle: "cookie")
auth_token = profile.gen_auth_token
group = Group.find_by(handle: "guildx")
Expand Down
125 changes: 123 additions & 2 deletions test/controllers/api/vote_controller_test.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,128 @@
require "test_helper"

class Api::VoteControllerTest < ActionDispatch::IntegrationTest
# test "the truth" do
# assert true
test "api#vote/create" do
profile = Profile.find_by(handle: "cookie")
auth_token = profile.gen_auth_token

assert_difference "VoteProposal.count", 1 do
post api_vote_create_url, params: {
auth_token: auth_token,
group_id: 1,
vote_proposal: {
title: "save the planet",
content: "we should save the planet",
max_choice: 1,
eligibile_group_id: 1,
start_time: DateTime.now,
end_time: DateTime.now + 7.days,
vote_options_attributes: [
{title: "yes", content: "yes we should save the planet"},
{title: "no", content: "no we should not save the planet"}
]
}
}
end
assert_response :success

vote = VoteProposal.find_by(title: "save the planet")
assert vote
end

test "api#vote/update" do
profile = Profile.find_by(handle: "cookie")
auth_token = profile.gen_auth_token
vote_proposal = VoteProposal.create!(
title: "save the planet",
content: "we should save the planet",
creator_id: profile.id,
group_id: 1,
max_choice: 1,
eligibile_group_id: 1,
start_time: DateTime.now,
end_time: DateTime.now + 7.days,
vote_options_attributes: [
{title: "yes", content: "yes we should save the planet"},
{title: "no", content: "no we should not save the planet"}
]
)

post api_vote_update_url, params: {
auth_token: auth_token,
id: vote_proposal.id,
group_id: 1,
vote_proposal: {
title: "save the oceans",
content: "we should save the oceans",
max_choice: 1,
eligibile_group_id: 1,
start_time: DateTime.now,
end_time: DateTime.now + 7.days,

}
}
assert_response :success

# vote_proposal.reload
# assert_equal "save the oceans", vote_proposal.title
# assert_equal "we should save the oceans", vote_proposal.content
# assert_equal "yes we should save the oceans", vote_proposal.vote_options.first.content
# assert_equal "no we should not save the oceans", vote_proposal.vote_options.second.content
end

# test "api#vote/cast_vote" do
# profile = Profile.find_by(handle: "cookie")
# auth_token = profile.gen_auth_token
# vote_proposal = VoteProposal.create!(
# title: "save the planet",
# content: "we should save the planet",
# creator_id: profile.id,
# group_id: 1,
# max_choice: 1,
# eligibile_group_id: 1,
# start_time: DateTime.now,
# end_time: DateTime.now + 7.days,
# vote_options_attributes: [
# {title: "yes", content: "yes we should save the planet"},
# {title: "no", content: "no we should not save the planet"}
# ]
# )

# post api_vote_cast_vote_url, params: {
# auth_token: auth_token,
# vote_proposal_id: vote_proposal.id,
# vote_option_ids: [vote_proposal.vote_options.first.id]
# }
# assert_response :success

# vote = VoteRecord.find_by(voter: profile, vote_proposal: vote_proposal)
# assert vote
# assert_equal vote.vote_options.first.title, "yes"
# end

# test "api#vote/cancel" do
# profile = Profile.find_by(handle: "cookie")
# auth_token = profile.gen_auth_token
# vote_proposal = VoteProposal.create!(
# title: "save the forests",
# content: "we should save the forests",
# max_choice: 1,
# eligibile_group_id: 1,
# start_time: DateTime.now,
# end_time: DateTime.now + 7.days,
# vote_options_attributes: [
# {title: "yes", content: "yes we should save the forests"},
# {title: "no", content: "no we should not save the forests"}
# ]
# )
# vote = Vote.create!(profile: profile, vote_proposal: vote_proposal, vote_option_ids: [vote_proposal.vote_options.first.id])

# post api_vote_cancel_url, params: {
# auth_token: auth_token,
# vote_id: vote.id
# }
# assert_response :success

# assert_nil Vote.find_by(id: vote.id)
# end
end
2 changes: 1 addition & 1 deletion test/controllers/api/voucher_controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ class Api::VoucherControllerTest < ActionDispatch::IntegrationTest
assert_response :success
assert_equal Voucher.find_by(badge_title: "super hello").counter, 1

# test accept badge
# test reject badge
profile2 = profiles(:two)
auth_token2 = profile2.gen_auth_token
post api_voucher_reject_badge_url, params: {
Expand Down

0 comments on commit c22455d

Please sign in to comment.