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 AcsClientCredentialsFlow(_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["scope"], _tokenRequest.Scope);
        }
        public void RequestAccessToken_RequestUrlIsCorrect()
        {
            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 AcsClientCredentialsFlow(_serviceUri, _tokenRequest, httpAdapter);

            // Act
            flow.RequestAccessToken();

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

            Uri expectedUri = new Uri(_serviceUri, "/v2/oauth2-13/");

            Assert.AreEqual(expectedUri, receivedRequest.Url.BuildUri());
        }
        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 AcsClientCredentialsFlow(_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);
        }