예제 #1
0
        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 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 GetShotInvalidBody()
        {
            var mock = RequestResponseMocker.Create("dummy {");
            var api  = new Players.RandomPlayerApi(config.JsonOptions, config.Serializer);
            await api.GetShots(mock.RequestMock.Object);

            Assert.Equal(HttpStatusCode.BadRequest, mock.ResponseMock.Object.StatusCode);
        }
        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 void GetReadyRandomPlayer()
        {
            var mock = RequestResponseMocker.Create();

            var api = new Players.RandomPlayerApi(config.JsonOptions, config.Serializer);

            api.GetReady(mock.RequestMock.Object);

            Assert.Equal(HttpStatusCode.OK, mock.ResponseMock.Object.StatusCode);
        }
        public async Task CreateResponse()
        {
            var api = new Api();

            var data         = new PlayerAddDto(Guid.Empty, "foo", "http://dummy.com");
            var responseMock = RequestResponseMocker.Create(data, api.jsonOptions);

            var response = await api.CreateResponse(responseMock.RequestMock.Object, "dummy", HttpStatusCode.BadRequest);

            Assert.NotNull(response);
            Assert.Equal(HttpStatusCode.BadRequest, response.StatusCode);
        }
        public async Task DeserializeAndValidateBodyInvalid()
        {
            var api = new Api();

            var responseMock = RequestResponseMocker.Create("dummy {", api.jsonOptions);

            var(item, response) = await api.DeserializeAndValidateBody <PlayerAddDto>(responseMock.RequestMock.Object);

            Assert.Null(item);
            Assert.NotNull(response);
            Assert.Equal(HttpStatusCode.BadRequest, responseMock.ResponseMock.Object.StatusCode);
        }
예제 #8
0
        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 DeserializeAndValidateBody()
        {
            var api = new Api();

            var data         = new PlayerAddDto(Guid.Empty, "foo", "http://dummy.com");
            var responseMock = RequestResponseMocker.Create(data, api.jsonOptions);

            var(item, response) = await api.DeserializeAndValidateBody <PlayerAddDto>(responseMock.RequestMock.Object);

            Assert.NotNull(item);
            Assert.Null(response);
            Assert.Equal(data, item);
        }
예제 #10
0
        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 GetShots()
        {
            var shotRequests = new[]
            {
                new ShotRequest(null, new BoardContent().ToShortString())
            };
            var mock = RequestResponseMocker.Create(JsonSerializer.Serialize(shotRequests, config.JsonOptions));
            var api  = new Players.RandomPlayerApi(config.JsonOptions, config.Serializer);
            await api.GetShots(mock.RequestMock.Object);

            JsonSerializer.Deserialize <BoardIndex[]>(mock.ResponseBodyAsString, config.JsonOptions);
            Assert.Equal(HttpStatusCode.OK, mock.ResponseMock.Object.StatusCode);
        }
예제 #12
0
        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);
        }
예제 #15
0
        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 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);
        }
        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);
        }