Commit 139b3a92 authored by Jason Williams's avatar Jason Williams
Browse files

Adds a test doing two things:

- Verifying the DevicePortalException parsing of a failure.
- Showing an example of a unittest using a string mock response instead of a file.
parent de20c7dd
Loading
Loading
Loading
Loading
+2 −0
Original line number Diff line number Diff line
@@ -40,6 +40,8 @@ namespace Microsoft.Tools.WindowsDevicePortal.Tests
        /// <param name="response">The response to return.</param>
        public void AddMockResponse(string endpoint, HttpResponseMessage response)
        {
            Utilities.ModifyEndpointForFilename(ref endpoint);

            this.mockResponses.Add(endpoint.ToLowerInvariant(), response);
        }

+44 −1
Original line number Diff line number Diff line
@@ -4,6 +4,7 @@
// </copyright>
//----------------------------------------------------------------------------------------------

using System;
using System.Collections.Generic;
using System.Net;
using System.Net.Http;
@@ -62,7 +63,7 @@ namespace Microsoft.Tools.WindowsDevicePortal.Tests
        public void UpdateXboxLiveUsersTest()
        {
            HttpResponseMessage response = new HttpResponseMessage(HttpStatusCode.NoContent);
            TestHelpers.MockHttpResponder.AddMockResponse(DevicePortal.XboxLiveUserApi);
            TestHelpers.MockHttpResponder.AddMockResponse(DevicePortal.XboxLiveUserApi, response);

            UserList users = new UserList();
            UserInfo user = new UserInfo();
@@ -76,5 +77,47 @@ namespace Microsoft.Tools.WindowsDevicePortal.Tests

            Assert.AreEqual(TaskStatus.RanToCompletion, updateUsersTask.Status);
        }

        /// <summary>
        /// Tests the failure case of trying to add a sponsored user
        /// when the maximum number are already on the console.
        /// </summary>
        [TestMethod]
        public void AddSponsoredUserTest_Failure()
        {
            HttpResponseMessage response = new HttpResponseMessage((HttpStatusCode)422);
            HttpContent content = new StringContent(
                "{\"ErrorCode\":-2136866553,\"ErrorMessage\":\"The maximum number of sponsored users are already signed in.\"}", 
                System.Text.Encoding.UTF8, 
                "application/json");

            response.Content = content;

            TestHelpers.MockHttpResponder.AddMockResponse(DevicePortal.XboxLiveUserApi, response);

            UserList users = new UserList();
            UserInfo user = new UserInfo();
            user.SponsoredUser = true;
            users.Add(user);

            try
            {
                Task updateUsersTask = TestHelpers.Portal.UpdateXboxLiveUsers(users);
                updateUsersTask.Wait();

                Assert.Fail("Expected an exception due to mock responder returning failure HRESULT.");
            }
            catch (Exception e)
            {
                Assert.IsTrue(e is AggregateException);
                Assert.IsNotNull(e.InnerException);
                Assert.IsTrue(e.InnerException is DevicePortalException);

                DevicePortalException exception = e.InnerException as DevicePortalException;

                Assert.AreEqual(-2136866553, exception.HResult);
                Assert.AreEqual("The maximum number of sponsored users are already signed in.", exception.Reason);
            }
        }
    }
}
+1 −1
Original line number Diff line number Diff line
@@ -43,7 +43,7 @@ namespace Microsoft.Tools.WindowsDevicePortal
            Exception innerException = null) : this(
                                                    responseMessage.StatusCode,
                                                    responseMessage.ReasonPhrase,
                                                    responseMessage.RequestMessage.RequestUri,
                                                    responseMessage.RequestMessage != null? responseMessage.RequestMessage.RequestUri : null,
                                                    message,
                                                    innerException)
        {