public async Task PlaySendsMessage()
        {
            var leaseManagerMock = new Mock <IPlayerGameLeaseManager>();

            leaseManagerMock.Setup(m => m.Acquire(Guid.Empty, It.IsAny <TimeSpan>())).ReturnsAsync("lease");

            var logTableMock = new Mock <IPlayerLogTable>();

            logTableMock.Setup(m => m.Add(It.IsAny <PlayerLog>())).ReturnsAsync(new PlayerLog(Guid.Empty));

            var senderMock = new Mock <IMessageSender>();

            senderMock.Setup(m => m.SendMessage(
                                 It.IsAny <PlayersPlayApi.MeasurePlayerRequestMessage>(), It.IsAny <string>(),
                                 It.IsAny <string>(), null));

            var playerMock = CreatePlayerTable();
            var authMock   = AuthorizeMocker.GetAuthorizeMock("foo");
            var mock       = RequestResponseMocker.Create();
            var result     = await CreateApi(playerMock, authMock, leaseManager : leaseManagerMock,
                                             logMock : logTableMock, messageSender : senderMock)
                             .Play(mock.RequestMock.Object, Guid.Empty.ToString());

            leaseManagerMock.VerifyAll();
            senderMock.VerifyAll();
            Assert.Equal(HttpStatusCode.Accepted, mock.ResponseMock.Object.StatusCode);
        }
        public async Task Me()
        {
            var payload = new User("foo")
            {
                NickName = "bar", Email = "*****@*****.**", PublicTwitter = "@foobar", PublicUrl = "https://foobar.com/profile"
            };
            var usersMock = new Mock <IUsersTable>();

            usersMock.Setup(p => p.GetSingle("foo")).Returns(Task.FromResult <User?>(payload));

            var mock = RequestResponseMocker.Create();

            await CreateApi(usersMock, AuthorizeMocker.GetAuthorizeMock("foo")).Me(mock.RequestMock.Object);

            var resultPayload = JsonSerializer.Deserialize <UserGetDto>(mock.ResponseBodyAsString, config.JsonOptions);

            usersMock.VerifyAll();
            Assert.Equal(HttpStatusCode.OK, mock.ResponseMock.Object.StatusCode);
            Assert.StartsWith("application/json", mock.Headers.First(h => h.Key == "Content-Type").Value.First());
            Assert.NotNull(resultPayload);
            Assert.Equal(payload.RowKey, resultPayload !.Subject);
            Assert.Equal(payload.Email, resultPayload !.Email);
            Assert.Equal(payload.NickName, resultPayload !.NickName);
            Assert.Equal(payload.PublicTwitter, resultPayload !.PublicTwitter);
            Assert.Equal(payload.PublicUrl, resultPayload !.PublicUrl);
        }
        public async Task TestChecksAuthorization()
        {
            var authMock = AuthorizeMocker.GetUnauthorizedMock();
            var mock     = RequestResponseMocker.Create();

            await CreateApi(new Mock <IPlayerTable>(), authMock).Test(mock.RequestMock.Object, Guid.Empty.ToString());

            authMock.VerifyAll();
            Assert.Equal(HttpStatusCode.Unauthorized, mock.ResponseMock.Object.StatusCode);
        }
        public async Task MeNotRegistered()
        {
            var usersMock = new Mock <IUsersTable>();

            usersMock.Setup(p => p.GetSingle("foo"));

            var mock = RequestResponseMocker.Create();

            await CreateApi(usersMock, AuthorizeMocker.GetAuthorizeMock("foo")).Me(mock.RequestMock.Object);

            usersMock.VerifyAll();
            Assert.Equal(HttpStatusCode.NotFound, mock.ResponseMock.Object.StatusCode);
        }
        public async Task RegisterInvalidBody()
        {
            var usersMock = new Mock <IUsersTable>();

            usersMock.Setup(p => p.Add(It.IsAny <User>()));

            var mock = RequestResponseMocker.Create("dummy {");

            await CreateApi(usersMock, AuthorizeMocker.GetAuthorizeMock("foo")).Add(mock.RequestMock.Object);

            usersMock.Verify(p => p.Add(It.IsAny <User>()), Times.Never);
            Assert.Equal(HttpStatusCode.BadRequest, mock.ResponseMock.Object.StatusCode);
        }
        public async Task RenewLease()
        {
            var leaseManagerMock = new Mock <IPlayerGameLeaseManager>();

            leaseManagerMock.Setup(m => m.Renew(Guid.Empty, "foo"));

            var api = CreateApi(new Mock <IPlayerTable>(), AuthorizeMocker.GetUnauthorizedMock(), leaseManager: leaseManagerMock);

            var msg = CreateDummyMeasurePlayerRequestMessage(DateTime.UtcNow.AddSeconds(-1));

            msg = await api.RenewLease(msg);

            leaseManagerMock.VerifyAll();
            Assert.True(msg.LeaseEnd > DateTime.UtcNow);
        }
        public async Task RegisterChecksAuthorization()
        {
            var authMock  = AuthorizeMocker.GetUnauthorizedMock();
            var usersMock = new Mock <IUsersTable>();

            usersMock.Setup(p => p.Add(It.IsAny <User>()));

            var mock = RequestResponseMocker.Create();

            await CreateApi(usersMock, authMock).Add(mock.RequestMock.Object);

            authMock.VerifyAll();
            usersMock.Verify(p => p.Add(It.IsAny <User>()), Times.Never);
            Assert.Equal(HttpStatusCode.Unauthorized, mock.ResponseMock.Object.StatusCode);
        }
        public async Task PlayTriesToAcquireLease()
        {
            var leaseManagerMock = new Mock <IPlayerGameLeaseManager>();

            leaseManagerMock.Setup(m => m.Acquire(Guid.Empty, It.IsAny <TimeSpan>()))
            .ThrowsAsync(new RequestFailedException("dummy"));

            var playerMock = CreatePlayerTable();
            var authMock   = AuthorizeMocker.GetAuthorizeMock("foo");
            var mock       = RequestResponseMocker.Create();

            await CreateApi(playerMock, authMock, leaseManager : leaseManagerMock)
            .Play(mock.RequestMock.Object, Guid.Empty.ToString());

            leaseManagerMock.VerifyAll();
            Assert.Equal(HttpStatusCode.Conflict, mock.ResponseMock.Object.StatusCode);
        }
        public async Task PlayDependencyErrorOnException()
        {
            var gameClientMock = new Mock <IGameClient>();

            gameClientMock.Setup(m => m.GetReadyForGame(It.IsAny <string>(), It.IsAny <string>()))
            .ThrowsAsync(new ApplicationException("Dummy Error"));

            var playerMock = CreatePlayerTable();
            var authMock   = AuthorizeMocker.GetAuthorizeMock("foo");
            var mock       = RequestResponseMocker.Create();

            await CreateApi(playerMock, authMock, gameClientMock : gameClientMock).Play(mock.RequestMock.Object, Guid.Empty.ToString());

            gameClientMock.VerifyAll();
            playerMock.VerifyAll();
            Assert.Equal(HttpStatusCode.FailedDependency, mock.ResponseMock.Object.StatusCode);
        }
        public async Task AlreadyRegistered()
        {
            var payload = new User("foo")
            {
                NickName = "bar", Email = "*****@*****.**", PublicTwitter = "@foobar", PublicUrl = "https://foobar.com/profile"
            };
            var usersMock = new Mock <IUsersTable>();

            usersMock.Setup(p => p.GetSingle("foo")).Returns(Task.FromResult <User?>(payload)).Verifiable();
            usersMock.Setup(p => p.Add(It.IsAny <User>()));

            var mock = RequestResponseMocker.Create();

            await CreateApi(usersMock, AuthorizeMocker.GetAuthorizeMock("foo")).Add(mock.RequestMock.Object);

            usersMock.Verify();
            usersMock.Verify(p => p.Add(It.IsAny <User>()), Times.Never);
            Assert.Equal(HttpStatusCode.Conflict, mock.ResponseMock.Object.StatusCode);
        }
        public async Task TestGetsReadyAndPlays()
        {
            var gameClientMock = new Mock <IGameClient>();

            gameClientMock.Setup(m => m.GetReadyForGame(It.IsAny <string>(), It.IsAny <string>()))
            .Returns(Task.CompletedTask);
            gameClientMock.Setup(m => m.PlaySingleMoveInRandomGame(It.IsAny <string>(), It.IsAny <string>()))
            .Returns(Task.CompletedTask);

            var playerMock = CreatePlayerTable();
            var authMock   = AuthorizeMocker.GetAuthorizeMock("foo");
            var mock       = RequestResponseMocker.Create();

            await CreateApi(playerMock, authMock, gameClientMock : gameClientMock)
            .Test(mock.RequestMock.Object, Guid.Empty.ToString());

            gameClientMock.VerifyAll();
            playerMock.VerifyAll();
            Assert.Equal(HttpStatusCode.OK, mock.ResponseMock.Object.StatusCode);
        }
        public async Task TestForeignPlayer()
        {
            var payload = new Player(Guid.Empty)
            {
                Name      = "Dummy",
                WebApiUrl = "https://somewhere.com/api",
                Creator   = "foo"
            };
            var playerMock = new Mock <IPlayerTable>();

            playerMock.Setup(p => p.GetSingle(It.IsAny <Guid>())).ReturnsAsync(payload);

            var mock = RequestResponseMocker.Create();

            await CreateApi(playerMock, AuthorizeMocker.GetAuthorizeMock("foo2"))
            .Test(mock.RequestMock.Object, Guid.Empty.ToString());

            playerMock.VerifyAll();
            Assert.Equal(HttpStatusCode.Forbidden, mock.ResponseMock.Object.StatusCode);
        }