<s> import datetime <EOL> import mock <EOL> from pyramid . testing import DummyRequest <EOL> import pytest <EOL> from h import db <EOL> from h . auth import models <EOL> from h . auth import tokens <EOL> def test_generate_jwt_calls_encode ( jwt ) : <EOL> """<STR_LIT>""" <EOL> before = datetime . datetime . utcnow ( ) <EOL> request = mock_request ( ) <EOL> tokens . generate_jwt ( request , <NUM_LIT> ) <EOL> assert jwt . encode . call_args [ <NUM_LIT:0> ] [ <NUM_LIT:0> ] [ '<STR_LIT>' ] == '<STR_LIT>' , ( <EOL> "<STR_LIT>" ) <EOL> after = datetime . datetime . utcnow ( ) + datetime . timedelta ( seconds = <NUM_LIT> ) <EOL> assert before < jwt . encode . call_args [ <NUM_LIT:0> ] [ <NUM_LIT:0> ] [ '<STR_LIT>' ] < after , ( <EOL> "<STR_LIT>" ) <EOL> assert jwt . encode . call_args [ <NUM_LIT:0> ] [ <NUM_LIT:0> ] [ '<STR_LIT>' ] == request . host_url , ( <EOL> "<STR_LIT>" ) <EOL> assert jwt . encode . call_args [ <NUM_LIT:1> ] [ '<STR_LIT>' ] == '<STR_LIT>' , ( <EOL> "<STR_LIT>" ) <EOL> def test_generate_jwt_when_authenticated_userid_is_None ( jwt ) : <EOL> """<STR_LIT>""" <EOL> request = mock_request ( ) <EOL> request . authenticated_userid = None <EOL> tokens . generate_jwt ( request , <NUM_LIT> ) <EOL> assert jwt . encode . call_args [ <NUM_LIT:0> ] [ <NUM_LIT:0> ] [ '<STR_LIT>' ] is None <EOL> def test_generate_jwt_returns_token ( jwt ) : <EOL> assert ( tokens . generate_jwt ( mock_request ( ) , <NUM_LIT> ) == <EOL> jwt . encode . return_value ) <EOL> def test_userid_from_jwt_calls_decode ( jwt ) : <EOL> request = mock_request ( ) <EOL> tokens . userid_from_jwt ( u'<STR_LIT>' , request ) <EOL> assert jwt . decode . call_args [ <NUM_LIT:0> ] == ( u'<STR_LIT>' , ) , ( <EOL> "<STR_LIT>" ) <EOL> assert ( jwt . decode . call_args [ <NUM_LIT:1> ] [ '<STR_LIT:key>' ] == <EOL> request . registry . settings [ '<STR_LIT>' ] ) , ( <EOL> "<STR_LIT>" ) <EOL> assert jwt . decode . call_args [ <NUM_LIT:1> ] [ '<STR_LIT>' ] == request . host_url , ( <EOL> "<STR_LIT>" ) <EOL> assert jwt . decode . call_args [ <NUM_LIT:1> ] [ '<STR_LIT>' ] == <NUM_LIT> , ( <EOL> "<STR_LIT>" ) <EOL> assert jwt . decode . call_args [ <NUM_LIT:1> ] [ '<STR_LIT>' ] == [ '<STR_LIT>' ] , ( <EOL> "<STR_LIT>" ) <EOL> def test_userid_from_jwt_returns_sub_from_decode ( jwt ) : <EOL> jwt . decode . return_value = { '<STR_LIT>' : '<STR_LIT>' } <EOL> result = tokens . userid_from_jwt ( u'<STR_LIT>' , mock_request ( ) ) <EOL> assert result == '<STR_LIT>' <EOL> def test_userid_from_jwt_returns_None_if_no_sub ( jwt ) : <EOL> jwt . decode . return_value = { } <EOL> result = tokens . userid_from_jwt ( u'<STR_LIT>' , mock_request ( ) ) <EOL> assert result is None <EOL> def test_userid_from_jwt_returns_None_if_decoding_fails ( jwt ) : <EOL> class InvalidTokenError ( Exception ) : <EOL> pass <EOL> jwt . InvalidTokenError = InvalidTokenError <EOL> jwt . decode . side_effect = InvalidTokenError <EOL> result = tokens . userid_from_jwt ( u'<STR_LIT>' , mock_request ( ) ) <EOL> assert result is None <EOL> def test_generate_jwt_userid_from_jwt_successful ( ) : <EOL> """<STR_LIT>""" <EOL> token = tokens . generate_jwt ( mock_request ( ) , <NUM_LIT> ) <EOL> userid = tokens . userid_from_jwt ( token , mock_request ( ) ) <EOL> assert userid == '<STR_LIT>' <EOL> def test_generate_jwt_userid_from_jwt_bad_token ( ) : <EOL> """<STR_LIT>""" <EOL> request = mock_request ( ) <EOL> request . registry . settings [ '<STR_LIT>' ] = '<STR_LIT>' <EOL> token = tokens . generate_jwt ( request , <NUM_LIT> ) <EOL> userid = tokens . userid_from_jwt ( token , mock_request ( ) ) <EOL> assert userid is None <EOL> def test_userid_from_api_token_returns_None_when_token_doesnt_start_with_prefix ( ) : <EOL> """<STR_LIT>""" <EOL> token = models . Token ( '<STR_LIT>' ) <EOL> token . value = u'<STR_LIT>' <EOL> db . Session . add ( token ) <EOL> result = tokens . userid_from_api_token ( u'<STR_LIT>' ) <EOL> assert result is None <EOL> def test_userid_from_api_token_returns_None_for_nonexistent_tokens ( ) : <EOL> madeuptoken = models . Token . prefix + '<STR_LIT>' <EOL> result = tokens . userid_from_api_token ( madeuptoken ) <EOL> assert result is None <EOL> def test_userid_from_api_token_returns_userid_for_valid_tokens ( ) : <EOL> token = models . Token ( '<STR_LIT>' ) <EOL> db . Session . add ( token ) <EOL> result = tokens . userid_from_api_token ( token . value ) <EOL> assert result == '<STR_LIT>' <EOL> def test_authenticated_userid_is_none_if_header_missing ( ) : <EOL> request = DummyRequest ( ) <EOL> assert tokens . authenticated_userid ( request ) is None <EOL> @ pytest . mark . parametrize ( '<STR_LIT:value>' , [ <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> ] ) <EOL> def test_authenticated_userid_is_none_if_header_incorrectly_formatted ( value ) : <EOL> request = DummyRequest ( headers = { '<STR_LIT>' : value } ) <EOL> assert tokens . authenticated_userid ( request ) is None <EOL> @ mock . patch ( '<STR_LIT>' ) <EOL> @ mock . patch ( '<STR_LIT>' ) <EOL> def test_authenticated_userid_passes_token_to_extractor_functions ( jwt , api_token ) : <EOL> api_token . return_value = None <EOL> jwt . return_value = None <EOL> request = DummyRequest ( headers = { '<STR_LIT>' : '<STR_LIT>' } ) <EOL> tokens . authenticated_userid ( request ) <EOL> api_token . assert_called_once_with ( '<STR_LIT>' ) <EOL> jwt . assert_called_once_with ( '<STR_LIT>' , request ) <EOL> @ mock . patch ( '<STR_LIT>' ) <EOL> @ mock . patch ( '<STR_LIT>' ) <EOL> def test_authenticated_userid_returns_userid_from_api_token_if_present ( jwt , api_token ) : <EOL> api_token . return_value = '<STR_LIT>' <EOL> jwt . return_value = '<STR_LIT>' <EOL> request = DummyRequest ( headers = { '<STR_LIT>' : '<STR_LIT>' } ) <EOL> result = tokens . authenticated_userid ( request ) <EOL> assert result == '<STR_LIT>' <EOL> @ mock . patch ( '<STR_LIT>' ) <EOL> @ mock . patch ( '<STR_LIT>' ) <EOL> def test_authenticated_userid_returns_userid_from_jwt_as_fallback ( jwt , api_token ) : <EOL> api_token . return_value = None <EOL> jwt . return_value = '<STR_LIT>' <EOL> request = DummyRequest ( headers = { '<STR_LIT>' : '<STR_LIT>' } ) <EOL> result = tokens . authenticated_userid ( request ) <EOL> assert result == '<STR_LIT>' <EOL> @ mock . patch ( '<STR_LIT>' ) <EOL> @ mock . patch ( '<STR_LIT>' ) <EOL> def test_authenticated_userid_returns_none_if_neither_token_valid ( jwt , api_token ) : <EOL> api_token . return_value = None <EOL> jwt . return_value = None <EOL> request = DummyRequest ( headers = { '<STR_LIT>' : '<STR_LIT>' } ) <EOL> result = tokens . authenticated_userid ( request ) <EOL> assert result is None <EOL> def mock_request ( token = None ) : <EOL> request = mock . Mock ( authenticated_userid = '<STR_LIT>' , <EOL> host_url = '<STR_LIT>' ) <EOL> request . registry . settings = { <EOL> '<STR_LIT>' : '<STR_LIT:id>' , <EOL> '<STR_LIT>' : '<STR_LIT>' <EOL> } <EOL> if token : <EOL> request . headers = { '<STR_LIT>' : token } <EOL> return request <EOL> @ pytest . fixture <EOL> def jwt ( patch ) : <EOL> return patch ( '<STR_LIT>' ) </s>
