public async Task GetUserProfile_NotAuthenticated() { var handlerMock = new Mock <HttpMessageHandler>(MockBehavior.Strict); handlerMock .Protected() .Setup <Task <HttpResponseMessage> >( "SendAsync", ItExpr.IsAny <HttpRequestMessage>(), ItExpr.IsAny <CancellationToken>() ) .ReturnsAsync(new HttpResponseMessage() { StatusCode = HttpStatusCode.OK, Content = new StringContent("{\"sub\": \"248289761001\",\"name\": \"Bob Smith\",\"preferred_username\": \"Bobby\",\"given_name\": \"Bob\",\"family_name\": \"Smith\",\"role\": [\"user\",\"admin\"]}"), }); IAuthentication auth = new BaseOAuthAuthenticationStub(handlerMock.Object); Assert.ThrowsAsync <NotAuthenticatedException>(async() => { User u = await auth.GetUserProfile(); }); handlerMock.VerifyNoOtherCalls(); }
public async Task GetUserProfile_Valid() { var handlerMock = new Mock <HttpMessageHandler>(MockBehavior.Strict); handlerMock .Protected() .Setup <Task <HttpResponseMessage> >( "SendAsync", ItExpr.IsAny <HttpRequestMessage>(), ItExpr.IsAny <CancellationToken>() ) .ReturnsAsync(new HttpResponseMessage() { StatusCode = HttpStatusCode.OK, Content = new StringContent("{\"sub\": \"248289761001\",\"name\": \"Bob Smith\",\"preferred_username\": \"Bobby\",\"given_name\": \"Bob\",\"family_name\": \"Smith\",\"role\": [\"user\",\"admin\"]}"), }) .Verifiable(); IAuthentication auth = new BaseOAuthAuthenticationStub(handlerMock.Object); await auth.Authenticate(); User profile = await auth.GetUserProfile(); handlerMock.Verify(); Assert.Multiple(() => { StringAssert.AreEqualIgnoringCase("Bob Smith", profile.Name, "Invalid name"); StringAssert.AreEqualIgnoringCase("248289761001", profile.UserId, "Invalid UserId"); StringAssert.AreEqualIgnoringCase("Bobby", profile.Username, "Invalid Username"); }); }