public void RequestAccessToken_ReceivedTokenIsCorrect()
        {
            JwtSecurityToken preparedToken = CreateJwtToken();
            ProtocolResponse oauthResponse = CreateJwtTokenResponse(preparedToken);
            IHttpClient      httpAdapter   = Substitute.For <IHttpClient>();

            httpAdapter.SendRequest(Arg.Any <ProtocolRequest>()).Returns(oauthResponse);

            var flow = new AzureAdClientCredentialsFlow(_serviceUri, _tokenRequest, httpAdapter);

            JwtSecurityToken receivedToken = flow.RequestAccessToken() as JwtSecurityToken;

            Assert.AreEqual(preparedToken.Audience, receivedToken.Audience);
            Assert.AreEqual(preparedToken.IssuedAt, receivedToken.IssuedAt);
            Assert.AreEqual(preparedToken.Issuer, receivedToken.Issuer);
            Assert.AreEqual(preparedToken.Expiration, receivedToken.Expiration);
        }
        public void RequestAccessToken_RequestContentIsCorrect()
        {
            JwtSecurityToken token         = CreateJwtToken();
            ProtocolResponse oauthResponse = CreateJwtTokenResponse(token);
            IHttpClient      httpAdapter   = Substitute.For <IHttpClient>();

            ProtocolRequest receivedRequest = null;

            httpAdapter.SendRequest(Arg.Do <ProtocolRequest>(request => receivedRequest = request))
            .Returns(oauthResponse);

            var flow = new AzureAdClientCredentialsFlow(_serviceUri, _tokenRequest, httpAdapter);

            flow.RequestAccessToken();

            httpAdapter.Received(1).SendRequest(Arg.Any <ProtocolRequest>());

            Assert.AreEqual(receivedRequest.BodyParameters["grant_type"], "client_credentials");
            Assert.AreEqual(receivedRequest.BodyParameters["client_id"], _tokenRequest.ClientId);
            Assert.AreEqual(receivedRequest.BodyParameters["client_secret"], _tokenRequest.ClientSecret);
            Assert.AreEqual(receivedRequest.BodyParameters["resource"], _tokenRequest.Scope);
        }
        public void RequestAccessToken_RequestUrlIsCorrect()
        {
            JwtSecurityToken token         = CreateJwtToken();
            ProtocolResponse oauthResponse = CreateJwtTokenResponse(token);
            IHttpClient      httpAdapter   = Substitute.For <IHttpClient>();

            // Return stub response and keep request for later inspection
            ProtocolRequest receivedRequest = null;

            httpAdapter.SendRequest(Arg.Do <ProtocolRequest>(request => receivedRequest = request))
            .Returns(oauthResponse);

            var flow = new AzureAdClientCredentialsFlow(_serviceUri, _tokenRequest, httpAdapter);

            // Act
            flow.RequestAccessToken();

            // Assert
            httpAdapter.Received(1).SendRequest(Arg.Any <ProtocolRequest>());

            Uri expectedUri = new Uri(_serviceUri, _serviceUri.AbsolutePath + "/oauth2/token");

            Assert.AreEqual(expectedUri, receivedRequest.Url.BuildUri());
        }