# Path: recommender/xsrf.py
# _TOKEN_TTL_SECONDS = datetime.timedelta(hours=2).total_seconds()
# def GenerateToken(secret, user_id, action, valid_until_timestamp=None):
# def ValidateToken(secret, token, user_id, action, now_timestamp=None):
# def _CreateKey(secret, user_id, action, valid_until_timestamp):


from recommender import xsrf
import time
import datetime
import unittest
# Copyright 2020 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.




class XsrfTest(unittest.TestCase):

  def testToken(self):
    token = xsrf.GenerateToken('secret1', 'user1', 'action1')
    self.assertTrue(xsrf.ValidateToken('secret1', token, 'user1', 'action1'))

    # Changing any part will make the token invalid.
    self.assertFalse(xsrf.ValidateToken('secret2', token, 'user1', 'action1'))

# Path: recommender/xsrf.py
# _TOKEN_TTL_SECONDS = datetime.timedelta(hours=2).total_seconds()
# def GenerateToken(secret, user_id, action, valid_until_timestamp=None):
# def ValidateToken(secret, token, user_id, action, now_timestamp=None):
# def _CreateKey(secret, user_id, action, valid_until_timestamp):


# Path: recommender/xsrf_test.py
from recommender import xsrf
import time
import datetime
import unittest

# Copyright 2020 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.




class XsrfTest(unittest.TestCase):

  def testToken(self):
    token = xsrf.GenerateToken('secret1', 'user1', 'action1')
    self.assertTrue(xsrf.ValidateToken('secret1', token, 'user1', 'action1'))

    # Changing any part will make the token invalid.
    self.assertFalse(xsrf.ValidateToken('secret2', token, 'user1', 'action1'))

    self.assertFalse(xsrf.ValidateToken('secret1', token, 'user2', 'action1'))