Skip to content

Commit

Permalink
add group_invite controller test
Browse files Browse the repository at this point in the history
  • Loading branch information
jiangplus committed Sep 10, 2024
1 parent 1e71294 commit 5b3a539
Show file tree
Hide file tree
Showing 3 changed files with 193 additions and 12 deletions.
14 changes: 7 additions & 7 deletions app/controllers/api/group_invite_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ def accept_request
if membership
membership.update(role: group_invite.role)
else
membership = Membership.create(profile_id: group_invite.receiver_id, group_id: group.id, role: group_invite.role)
membership = Membership.create(profile_id: group_invite.receiver_id, group_id: group.id, role: group_invite.role, status: "active")
group.increment!(:memberships_count)
end

Expand All @@ -56,8 +56,8 @@ def send_invite
expires_at = (DateTime.now + 30.days)

group_invites = []
params[:receivers].map do |receiver|
receiver = Profile.find_by(address: receiver) || Profile.find_by(handle: receiver) || Profile.find_by(email: receiver)
params[:receivers].map do |receiver_address|
receiver = Profile.find_by(address: receiver_address) || Profile.find_by(handle: receiver_address) || Profile.find_by(email: receiver_address)

if receiver
receiver_id = receiver.id
Expand All @@ -84,7 +84,7 @@ def send_invite
# todo : membership uniqueness
# todo : test existing member and new member
# todo : test existing member with manager or owner role
elsif receiver.include? "@"
elsif receiver_address.include? "@"

invite = GroupInvite.create(
sender_id: profile.id,
Expand All @@ -93,11 +93,11 @@ def send_invite
role: role,
expires_at: expires_at,
receiver_address_type: "email",
receiver_address: receiver,
receiver_address: receiver_address,
)

mailer = GroupMailer.with(group_name: (group.nickname || group.handle), recipient: invite.receiver_address).group_invite_email
mailer.deliver_now!
# mailer = GroupMailer.with(group_name: (group.nickname || group.handle), recipient: invite.receiver_address).group_invite_email
# mailer.deliver_now!
else
invite = { receiver: receiver, result: "error", message: "invalid receiver handle" }
end
Expand Down
188 changes: 185 additions & 3 deletions test/controllers/api/group_invite_controller_test.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,189 @@
require "test_helper"

class Api::GroupInviteControllerTest < ActionDispatch::IntegrationTest
# test "the truth" do
# assert true
# end

test "api/group/send_invite" do
profile = Profile.find_by(handle: "cookie")
auth_token = profile.gen_auth_token
group = Group.find_by(handle: "guildx")

assert_difference 'GroupInvite.count', 1 do
post api_group_send_invite_url, params: {
auth_token: auth_token,
group_id: group.id,
role: "member",
receivers: ["mooncake"],
message: "please join the group"
}
end
assert_response :success
group_invite = GroupInvite.find_by(group: group, sender: profile)
assert group_invite.status == "sending"
end

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
group = Group.find_by(handle: "guildx")

Membership.create(profile_id: profile2.id, group_id: group.id, role: "member", status: "active")

assert_difference 'GroupInvite.count', 0 do
post api_group_send_invite_url, params: {
auth_token: auth_token,
group_id: group.id,
role: "manager",
receivers: ["mooncake"],
message: "please join the group"
}
end
assert_response :success
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
profile = Profile.find_by(handle: "cookie")
profile2 = Profile.find_by(handle: "mooncake")
auth_token = profile.gen_auth_token
group = Group.find_by(handle: "guildx")

Membership.create(profile_id: profile2.id, group_id: group.id, role: "manager", status: "active")

assert_difference 'GroupInvite.count', 0 do
post api_group_send_invite_url, params: {
auth_token: auth_token,
group_id: group.id,
role: "member",
receivers: ["mooncake"],
message: "please join the group"
}
end
assert_response :success
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
profile = Profile.find_by(handle: "cookie")
profile2 = Profile.find_by(handle: "dimsum")
auth_token = profile.gen_auth_token
group = Group.find_by(handle: "guildx")

assert_difference 'GroupInvite.count', 1 do
post api_group_send_invite_url, params: {
auth_token: auth_token,
group_id: group.id,
role: "manager",
receivers: ["[email protected]"],
message: "please join the group"
}
end
assert_response :success

profile2.update(email: "[email protected]")
auth_token2 = profile2.gen_auth_token
invite = GroupInvite.find_by(group: group, sender: profile)

post api_group_accept_invite_url, params: {
auth_token: auth_token2,
group_invite_id: invite.id
}
assert_response :success

group_member = Membership.find_by(group: group, profile: profile2)
assert group_member
assert_equal "manager", group_member.role
end

test "api/group/accept_invite" do
profile = Profile.find_by(handle: "mooncake")
auth_token = profile.gen_auth_token
group = Group.find_by(handle: "guildx")
invite = GroupInvite.create!(group: group, sender: profiles(:one), receiver: profile, expires_at: DateTime.now + 30.days, role: "member", message: "please join the group")

post api_group_accept_invite_url, params: {
auth_token: auth_token,
group_invite_id: invite.id
}
assert_response :success

group_member = Membership.find_by(group: group, profile: profile)
assert group_member
assert_equal "member", group_member.role
end

test "api/group/cancel_invite" do
profile = Profile.find_by(handle: "mooncake")
auth_token = profile.gen_auth_token
group = Group.find_by(handle: "guildx")
invite = GroupInvite.create!(group: group, sender: profiles(:one), receiver: profile, expires_at: DateTime.now + 30.days, role: "member", message: "please join the group")

post api_group_cancel_invite_url, params: {
auth_token: auth_token,
group_invite_id: invite.id
}
assert_response :success

group_invite = GroupInvite.find_by(group: group, receiver: profile)
assert group_invite.status == "cancelled"
end

test "api/group/revoke_invite" do
profile = Profile.find_by(handle: "cookie")
auth_token = profile.gen_auth_token
group = Group.find_by(handle: "guildx")
invite = GroupInvite.create!(group: group, sender: profiles(:one), receiver: profile, expires_at: DateTime.now + 30.days, role: "member", message: "please join the group")

post api_group_revoke_invite_url, params: {
auth_token: auth_token,
group_invite_id: invite.id
}
assert_response :success

group_invite = GroupInvite.find_by(group: group, receiver: profile)
assert group_invite.status == "cancelled"
end

test "api/group/request_invite" do
profile = Profile.find_by(handle: "mooncake")
auth_token = profile.gen_auth_token
group = Group.find_by(handle: "guildx")

post api_group_request_invite_url, params: {
auth_token: auth_token,
group_id: group.id,
role: "member",
message: "I would like to join the group"
}
assert_response :success

group_invite = GroupInvite.find_by(group: group, receiver: profile)
assert group_invite
assert_equal "requesting", group_invite.status
assert_equal "member", group_invite.role
assert_equal "I would like to join the group", group_invite.message

end

test "api/group/accept_request" do
profile = Profile.find_by(handle: "cookie")
auth_token = profile.gen_auth_token
group = Group.find_by(handle: "guildx")
requester = profiles(:two)
group_invite = GroupInvite.create!(group: group, sender: profile, receiver: requester, expires_at: DateTime.now + 30.days, role: "member", message: "please join the group", status: "requesting")

post api_group_accept_request_url, params: {
auth_token: auth_token,
group_invite_id: group_invite.id
}
assert_response :success

group_invite.reload
assert_equal "accepted", group_invite.status

membership = Membership.find_by(profile_id: requester.id, group_id: group.id)
assert membership
assert_equal "member", membership.role
end

end
3 changes: 1 addition & 2 deletions test/fixtures/memberships.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,9 @@
# below each fixture, per the syntax in the comments below
#
one:
id: 1
profile_id: 1
group_id: 1
role: "owner"
status: "active"

two: {}
# column: value

0 comments on commit 5b3a539

Please sign in to comment.