示例#1
0
        public void GetProfileCharacters_CrossSaveOverrideSearchForXboxAccount_ShouldThrowApiExceptionDestinyAccountNotFound()
        {
            IConfiguration            config              = Mock.Of <IConfiguration>(m => m["Bungie:ApiKey"] == "dummy-api-key");
            int                       membershipType      = 1;
            long                      destinyMembershipId = 4611686018497175745;
            Uri                       uri            = new Uri($"https://www.bungie.net/Platform/Destiny2/{membershipType}/Profile/{destinyMembershipId}/?components=200");
            string                    responseString = TestUtils.ReadFile("GetProfile-xbox-exists-but-not-found-because-cross-save-override.json");
            Mock <HttpMessageHandler> mock           = new Mock <HttpMessageHandler>(MockBehavior.Strict);

            mock
            .Protected()
            .Setup <Task <HttpResponseMessage> >(
                "SendAsync",
                ItExpr.Is <HttpRequestMessage>(
                    req =>
                    req.Method == HttpMethod.Get &&
                    req.RequestUri == uri &&
                    req.Headers.Any(h => h.Key == "X-API-KEY" && !string.IsNullOrEmpty(h.Value.FirstOrDefault()))),
                ItExpr.IsAny <CancellationToken>())
            .ReturnsAsync(new HttpResponseMessage()
            {
                StatusCode = HttpStatusCode.InternalServerError,
                Content    = new StringContent(responseString)
            })
            .Verifiable();
            HttpClient               httpClient       = new HttpClient(mock.Object);
            BungieApiService         bungieApiService = new BungieApiService(config, httpClient);
            ProfileCharactersRequest request          = new ProfileCharactersRequest(membershipType, destinyMembershipId);
            // act (and assert)
            var exception = Assert.ThrowsException <BungieApiException>(() => bungieApiService.GetProfileCharacters(request));

            Assert.IsTrue(exception.Message.Contains("DestinyAccountNotFound"));
        }
示例#2
0
        public void GetProfileCharacters_PersonalAccount_ShouldReturnAllCharacters()
        {
            IConfiguration            config              = Mock.Of <IConfiguration>(m => m["Bungie:ApiKey"] == "dummy-api-key");
            int                       membershipType      = 3;
            long                      destinyMembershipId = 4611686018467260757;
            Uri                       uri            = new Uri($"https://www.bungie.net/Platform/Destiny2/{membershipType}/Profile/{destinyMembershipId}/?components=200");
            string                    responseString = TestUtils.ReadFile("GetProfile-valid-personal.json");
            Mock <HttpMessageHandler> mock           = new Mock <HttpMessageHandler>(MockBehavior.Strict);

            mock
            .Protected()
            .Setup <Task <HttpResponseMessage> >(
                "SendAsync",
                ItExpr.Is <HttpRequestMessage>(
                    req =>
                    req.Method == HttpMethod.Get &&
                    req.RequestUri == uri &&
                    req.Headers.Any(h => h.Key == "X-API-KEY" && !string.IsNullOrEmpty(h.Value.FirstOrDefault()))),
                ItExpr.IsAny <CancellationToken>())
            .ReturnsAsync(new HttpResponseMessage()
            {
                StatusCode = HttpStatusCode.OK,
                Content    = new StringContent(responseString)
            })
            .Verifiable();
            HttpClient       httpClient       = new HttpClient(mock.Object);
            BungieApiService bungieApiService = new BungieApiService(config, httpClient);

            DestinyCharacter expectedCharacter = new DestinyCharacter();

            expectedCharacter.CharacterId           = 2305843009504575107;
            expectedCharacter.DateLastPlayed        = DateTimeOffset.Parse("2019-12-24T22:40:31Z");
            expectedCharacter.DestinyProfileId      = 4611686018467260757;
            expectedCharacter.DestinyMembershipType = 3;

            ProfileCharactersRequest request = new ProfileCharactersRequest(membershipType, destinyMembershipId);
            // act
            ProfileCharactersResponse actual = bungieApiService.GetProfileCharacters(request);

            // assert
            Assert.AreEqual(3, actual.Characters.Values.Count);
            Assert.IsTrue(actual.Characters.ContainsKey(expectedCharacter.CharacterId));
            DestinyCharacter actualCharacter = actual.Characters[expectedCharacter.CharacterId];

            Assert.AreEqual(expectedCharacter.CharacterId, actualCharacter.CharacterId);
            Assert.AreEqual(expectedCharacter.DateLastPlayed, actualCharacter.DateLastPlayed);
            Assert.AreEqual(expectedCharacter.DestinyProfileId, actualCharacter.DestinyProfileId);
            Assert.AreEqual(expectedCharacter.DestinyMembershipType, actualCharacter.DestinyMembershipType);
        }