From 5b3a539f5488c7d823d8c945f1439872f0eeed6d Mon Sep 17 00:00:00 2001 From: jiangplus Date: Tue, 10 Sep 2024 22:13:54 +0800 Subject: [PATCH] add group_invite controller test --- .../api/group_invite_controller.rb | 14 +- .../api/group_invite_controller_test.rb | 188 +++++++++++++++++- test/fixtures/memberships.yml | 3 +- 3 files changed, 193 insertions(+), 12 deletions(-) diff --git a/app/controllers/api/group_invite_controller.rb b/app/controllers/api/group_invite_controller.rb index ee1c4dc..f13a316 100644 --- a/app/controllers/api/group_invite_controller.rb +++ b/app/controllers/api/group_invite_controller.rb @@ -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 @@ -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 @@ -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, @@ -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 diff --git a/test/controllers/api/group_invite_controller_test.rb b/test/controllers/api/group_invite_controller_test.rb index 4285f0e..361a6dd 100644 --- a/test/controllers/api/group_invite_controller_test.rb +++ b/test/controllers/api/group_invite_controller_test.rb @@ -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: ["dimsum@mail.com"], + message: "please join the group" + } + end + assert_response :success + + profile2.update(email: "dimsum@mail.com") + 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 diff --git a/test/fixtures/memberships.yml b/test/fixtures/memberships.yml index d89c1b1..55443ed 100644 --- a/test/fixtures/memberships.yml +++ b/test/fixtures/memberships.yml @@ -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