--[[----------------------------------------------------------------------------

WFOAuth2SessionImpTest.lua

--------------------------------------------------------------------------------

ADOBE CONFIDENTIAL
------------------
Copyright 2015 Adobe Systems Incorporated
All Rights Reserved.

NOTICE: All information contained herein is, and remain the property of
Adobe Systems Incorporated and its suppliers, if any. The intellectual and
technical concepts contained herein are proprietary to Adobe Systems
Incorporated and its suppliers and may be covered by U.S. and Foreign
Patents, patents in process, and are protected by trade secret or copyright
law.  Dissemination of this information or reproduction of this material is
strictly forbidden unless prior written permission is obtained from
Adobe Systems Incorporated.

------------------------------------------------------------------------------]]

	-- WF.core.self_test
local WFRxTestCase = require 'WFRxTestCase'

	-- WF.web.http_client
local WFRxHttpClientMock = require 'WFRxHttpClientMock'

	-- WF.web.oauth2_client
local WFOAuth2SessionImp = require 'WFOAuth2SessionImp'
local WFOAuth2SessionTestFactory = require 'WFOAuth2SessionTestFactory'

	-- WF.oz_client.ims
local WFImsAppIdentity = require 'WFImsAppIdentity'

--============================================================================--

local WFOAuth2SessionImpTest = WFOAuth2SessionTestFactory( WFOAuth2SessionImp )

local setUpOAuth2Session = assert( WFOAuth2SessionImpTest.setUpOAuth2Session )

--------------------------------------------------------------------------------

function WFOAuth2SessionImpTest.rxTestRefreshToken_tokenRequestFails_testNightly( rxTest )

	rxTest:setTimeLimit( 60 )

	local imsClientId = WFImsAppIdentity.getClientIdAndSecret()
	
	local session = setUpOAuth2Session( rxTest, WFOAuth2SessionImp( 'testUser' ) )

	rxTest:expectOnlyCompleted( session:storeTokens( 'access', 'refresh' ) )

	local mock = WFRxHttpClientMock( rxTest, {

		{
			url = 'url://1',
			options = {
				requestHeaders = {
					{
						header = "Authorization",
						value = "Bearer access",
					},
					{
						header = "Accept-Language",
						value = "(language)",
					},
				},
				retainDataAfterRequest = true,
			},

			response = 'nope',
			headers = {
				status_code = '401',
				[ 'WWW-Authenticate' ] = 'url://auth?auth_url="auth://blah/authorize"',
			},

		},

		{
			url = "auth://blah/ims/token",
			data = "grant_type=refresh_token&refresh_token=refresh&client_id=(mumble)&client_secret=(secret)",

			options = {
				requestHeaders = {
					{
						header = "Content-Type",
						value = "application/x-www-form-urlencoded",
					},
					{
						header = "X-IMS-ClientID",
						value = imsClientId,
					}
				},
				requestMethod = "POST",
			},
			fail = "Network connection lost",
		},

		{
			url = "auth://blah/ims/token",
			data = "grant_type=refresh_token&refresh_token=refresh&client_id=(mumble)&client_secret=(secret)",

			options = {
				requestHeaders = {
					{
						header = "Content-Type",
						value = "application/x-www-form-urlencoded",
					},
					{
						header = "X-IMS-ClientID",
						value = imsClientId,
					}
				},
				requestMethod = "POST",
			},
			fail = "Network connection lost",
		},

		{
			url = "auth://blah/ims/token",
			data = "grant_type=refresh_token&refresh_token=refresh&client_id=(mumble)&client_secret=(secret)",

			options = {
				requestHeaders = {
					{
						header = "Content-Type",
						value = "application/x-www-form-urlencoded",
					},
					{
						header = "X-IMS-ClientID",
						value = imsClientId,
					}
				},
				requestMethod = "POST",
			},
			fail = "Network connection lost",
		},

	} )

	session:DEBUG_replaceHttpStack( mock )

	rxTest:expectOnlyError( session:get( 'url://1' ), "Unable to refresh tokens" )

	rxTest:expectEqual( 4, mock:requestCount() )

end

--------------------------------------------------------------------------------

function WFOAuth2SessionImpTest.rxTestRefreshToken_tokenRequestFailsThenSucceeds_testNightly( rxTest )

	rxTest:setTimeLimit( 60 )

	local imsClientId = WFImsAppIdentity.getClientIdAndSecret()
	
	local session = setUpOAuth2Session( rxTest, WFOAuth2SessionImp( 'testUser' ) )

	rxTest:expectOnlyCompleted( session:storeTokens( 'access', 'refresh' ) )

	local mock = WFRxHttpClientMock( rxTest, {

		{
			url = 'url://1',
			options = {
				requestHeaders = {
					{
						header = "Authorization",
						value = "Bearer access",
					},
					{
						header = "Accept-Language",
						value = "(language)",
					},
				},
				retainDataAfterRequest = true,
			},

			response = 'nope',
			headers = {
				status_code = '401',
				[ 'WWW-Authenticate' ] = 'url://auth?auth_url="auth://blah/authorize"',
			},

		},

		{
			url = "auth://blah/ims/token",
			data = "grant_type=refresh_token&refresh_token=refresh&client_id=(mumble)&client_secret=(secret)",

			options = {
				requestHeaders = {
					{
						header = "Content-Type",
						value = "application/x-www-form-urlencoded",
					},
					{
						header = "X-IMS-ClientID",
						value = imsClientId,
					}
				},
				requestMethod = "POST",
			},
			fail = "Network connection lost",
		},

		{
			url = "auth://blah/ims/token",
			data = "grant_type=refresh_token&refresh_token=refresh&client_id=(mumble)&client_secret=(secret)",

			options = {
				requestHeaders = {
					{
						header = "Content-Type",
						value = "application/x-www-form-urlencoded",
					},
					{
						header = "X-IMS-ClientID",
						value = imsClientId,
					}
				},
				requestMethod = "POST",
			},
			response = '{"access_token":"lemmein"}',
			headers = { status_code = '200' },
		},

		{
			url = "url://1",
			options = {
				requestHeaders = {
					{
						header = "Accept-Language",
						value = "(language)",
					},
					{
						header = "Authorization",
						value = "Bearer lemmein",
					},
				},
				retainDataAfterRequest = true,
			},

			response = "good stuff",
			headers = { status_code = '200' },
		},

	} )

	session:DEBUG_replaceHttpStack( mock )

	rxTest:expectSingleValue( session:get( 'url://1' ),
							  "good stuff",
							  { status_code = '200' } )

	rxTest:expectEqual( 4, mock:requestCount() )

end

--------------------------------------------------------------------------------

-- TODO [scouten 2015-07-20]: Review WFTokenTest for possible additional
-- test scenarios.

--------------------------------------------------------------------------------

return WFRxTestCase.wrapSuite( WFOAuth2SessionImpTest )
