public override async Task <GetInviteResponse> GetInvite(GetInviteRequest request, ServerCallContext context) { var playerId = AuthHeaders.ExtractPlayerId(context); if (string.IsNullOrEmpty(request.InviteId)) { throw new RpcException(new Status(StatusCode.InvalidArgument, "Expected non-empty invite id")); } using (var memClient = _memoryStoreClientManager.GetClient()) { var invite = await memClient.GetAsync <InviteDataModel>(request.InviteId) ?? throw new EntryNotFoundException(request.InviteId, "No such invite with the given id found"); if (!invite.PlayerInvolved(playerId)) { throw new RpcException(new Status(StatusCode.PermissionDenied, "The player is not involved in this invite")); } return(new GetInviteResponse { Invite = ConvertToProto(invite) }); } }
public void ReturnEntryNotFoundWhenInviteDoesNotExist() { // Setup the client such that it will claim there is no such invite with the given id. _mockMemoryStoreClient.Setup(client => client.GetAsync <InviteDataModel>(_storedInvite.Id)) .ReturnsAsync((InviteDataModel)null); // Verify that the request has finished without any errors being thrown. var context = Util.CreateFakeCallContext(SenderPlayerId, ""); var request = new GetInviteRequest { InviteId = _storedInvite.Id }; Assert.ThrowsAsync <EntryNotFoundException>(() => _inviteService.GetInvite(request, context)); }
public void ReturnPermissionsDeniedWhenThePlayerIsNotInvolvedInTheInvite() { // Setup the client such that it will claim the user making this request is not involved in the invite. _mockMemoryStoreClient.Setup(client => client.GetAsync <InviteDataModel>(_storedInvite.Id)) .ReturnsAsync(_storedInvite); // Check that player involvement is enforced by GetInvite. var context = Util.CreateFakeCallContext("SomeoneElse", ""); var request = new GetInviteRequest { InviteId = _storedInvite.Id }; var exception = Assert.ThrowsAsync <RpcException>(() => _inviteService.GetInvite(request, context)); Assert.AreEqual(StatusCode.PermissionDenied, exception.StatusCode); }
public void ReturnInviteWhenFound() { // Setup the client such that it will claim there is an associated invite with the given id. _mockMemoryStoreClient.Setup(client => client.GetAsync <InviteDataModel>(_storedInvite.Id)) .ReturnsAsync(_storedInvite); // Verify that the expected invite was returned as a response. var context = Util.CreateFakeCallContext(SenderPlayerId, ""); var request = new GetInviteRequest { InviteId = _storedInvite.Id }; var invite = _inviteService.GetInvite(request, context).Result.Invite; Assert.NotNull(invite); InviteComparator.AssertEquivalent(_storedInvite, invite); }
public void AllowTheReceiverToAcceptTheInvite() { // Create a party. var senderPit = CreatePlayerIdentityTokenForPlayer(LeaderPlayerId); _partyClient.CreateParty(new CreatePartyRequest(), new Metadata { { PitRequestHeaderName, senderPit } }); // Send invites to other players. var createInviteRequest = new CreateInviteRequest { ReceiverPlayerId = PlayerId1 }; var inviteId = _inviteClient .CreateInvite(createInviteRequest, new Metadata { { PitRequestHeaderName, senderPit } }) .InviteId; Assert.NotNull(inviteId); // Receiver accepts the invite. var receiverPit = CreatePlayerIdentityTokenForPlayer(PlayerId1); var getInviteRequest = new GetInviteRequest { InviteId = inviteId }; var invite = _inviteClient.GetInvite(getInviteRequest, new Metadata { { PitRequestHeaderName, receiverPit } }) .Invite; invite.CurrentStatus = Invite.Types.Status.Accepted; var updatedInvite = _inviteClient.UpdateInvite(new UpdateInviteRequest { UpdatedInvite = invite }, new Metadata { { PitRequestHeaderName, receiverPit } }).Invite; Assert.AreEqual(Invite.Types.Status.Accepted, updatedInvite.CurrentStatus); // Verify this change has propagated to the sender as well. var senderPlayerInvites = _inviteClient.ListAllInvites(new ListAllInvitesRequest(), new Metadata { { PitRequestHeaderName, senderPit } }); Assert.AreEqual(1, senderPlayerInvites.OutboundInvites.Count); Assert.AreEqual(updatedInvite, senderPlayerInvites.OutboundInvites[0]); // Clean up. _inviteClient.DeleteInvite(new DeleteInviteRequest { InviteId = inviteId }, new Metadata { { PitRequestHeaderName, receiverPit } }); _partyClient.DeleteParty(new DeletePartyRequest(), new Metadata { { PitRequestHeaderName, senderPit } }); }