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

WFOzUserLoginTest.lua

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

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

NOTICE: All information contained herein is, and remains 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 are protected by all applicable
intellectual property laws, including trade secret and copyright laws.
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.oz_client.account
local WFOzUserTestSession = require 'WFOzUserTestSession'

	-- WF.oz_client.managed_catalog
local WFCatUser = require 'WFCatUser'

	-- WF.oz_client.sync_engine.user
local WFOzUserLogin = require 'WFOzUserLogin'


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

-- @mark WFOzUserLoginTest

local WFOzUserLoginTest = {}

local ozV2StageServerUrl = 'https://photos-stage.adobe.io'

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

function WFOzUserLoginTest.rxTestLoginFromTokens_testNightly( rxTest )

	rxTest:setTimeLimit( 240 )

	-- Acquire a test account.

	local userTestSession = WFOzUserTestSession( rxTest )

	local userEmail, userPassword, userAccountDetails

	rxTest:expectSingleValue(

		userTestSession:userInfo()
			:tap( function( email, password, otherData )

				userEmail = email
				userPassword = password
				userAccountDetails = otherData

				debug.print( "Using " .. email .. " account to run tests" )

			end ),

		':ignore:', ':ignore:', ':ignore:' )

	-- Convert that to tokens.

	local baseServerUrl, accessToken, refreshToken

	rxTest:expectSingleValue(

		WFOzUserLogin.tokensFromEmailAndPassword( ozV2StageServerUrl, 'v2', userEmail, userPassword )
			:tap( function( _baseServerUrl, _accessToken, _refreshToken )

				baseServerUrl = _baseServerUrl
				accessToken = _accessToken
				refreshToken = _refreshToken

			end ),

		':ignore:', ':ignore:', ':ignore:' )

	-- Now attempt to log in with those credentials.

	rxTest:expectSingleValue(

		WFOzUserLogin.loginFromTokens( ':memory:', baseServerUrl, accessToken, refreshToken )

			:map( function( wfCatUser, userInfo )

				if not WFCatUser:isClassOf( wfCatUser ) then
					error "first arg isn't a WFCatUser"
				end

				local userId = wfCatUser:userId()

				local userIdStatus = type( userId ) == 'string'
					and userId:match( '^[0-9a-f]+$' )
					and #userId == 32

				local infoPacketError = false

				if not infoPacketError and type( userInfo ) ~= 'table' then
					infoPacketError = "type = " .. type( userInfo )
				end

				if not infoPacketError and userInfo._id ~= userId then
					infoPacketError = "_id should be " .. userId
				end

				if not infoPacketError and userInfo.email ~= userEmail then
					infoPacketError = "email should be " .. userEmail
				end

				return userIdStatus, userId, infoPacketError, userInfo

			end ),

		true, ':ignore:', false, ':ignore:' )

end

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

function WFOzUserLoginTest.rxTestLoginFromEmailAndPassword_testNightly( rxTest )

	rxTest:setTimeLimit( 240 )

	-- Acquire a test account.

	local userTestSession = WFOzUserTestSession( rxTest )

	local userEmail, userPassword, userAccountDetails

	rxTest:expectSingleValue(

		userTestSession:userInfo()
			:tap( function( email, password, otherData )

				userEmail = email
				userPassword = password
				userAccountDetails = otherData

				debug.print( "Using " .. email .. " account to run tests" )

			end ),

		':ignore:', ':ignore:', ':ignore:' )

	rxTest:expectSingleValue(

		WFOzUserLogin.loginFromEmailAndPassword( ':memory:', ozV2StageServerUrl,
												 userEmail, userPassword )

			:map( function( wfCatUser, userInfo )

				if not WFCatUser:isClassOf( wfCatUser ) then
					error "first arg isn't a WFCatUser"
				end

				local userId = wfCatUser:userId()

				local userIdStatus = type( userId ) == 'string'
					and userId:match( '^[0-9a-f]+$' )
					and #userId == 32

				local infoPacketError = false

				if not infoPacketError and type( userInfo ) ~= 'table' then
					infoPacketError = "type = " .. type( userInfo )
				end

				if not infoPacketError and userInfo._id ~= userId then
					infoPacketError = "_id should be " .. userId
				end

				if not infoPacketError and userInfo.email ~= userEmail then
					infoPacketError = "email should be " .. userEmail
				end

				return userIdStatus, userId, infoPacketError, userInfo

			end ),

			true, ':ignore:', false, ':ignore:' )

end

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

return WFRxTestCase.wrapSuite( 'WFOzUserLoginTest', WFOzUserLoginTest )
