<s> from biicode . server . test . store . mongo_test import TestWithMongo <EOL> from biicode . server . user . jwt_accounts_manager import JWTConfirmEmailManagerFactory <EOL> from biicode . server . user . user_service import UserService <EOL> from biicode . server . api . jwt_credentials_manager import JWTCredentialsManagerFactory <EOL> from time import sleep <EOL> from biicode . test . remote . testing_mongo_store import TestingMongoStore <EOL> from jwt import DecodeError <EOL> from mock import patch , Mock <EOL> from biicode . server . exception import ControledErrorException <EOL> class UserServiceTest ( TestWithMongo ) : <EOL> def setUp ( self , * args , ** kwargs ) : <EOL> self . store = TestingMongoStore ( self . conn , self . database_name ) <EOL> _ , self . brl_user = self . store . generate_non_persistent_user ( ) <EOL> self . user_service = UserService ( self . store , self . brl_user ) <EOL> self . email = "<STR_LIT>" % self . brl_user <EOL> self . plain_password = "<STR_LIT>" <EOL> allow_mailing = False <EOL> self . bii_user = self . user_service . register ( self . brl_user , self . email , <EOL> self . plain_password , allow_mailing ) <EOL> self . manager = JWTConfirmEmailManagerFactory . new ( ) <EOL> _ , self . confirmed_user , _ = self . user_service . confirm_account ( self . bii_user . confirmation_token ) <EOL> def test_used_register ( self ) : <EOL> '''<STR_LIT>''' <EOL> self . assertEqual ( self . bii_user . ID , self . confirmed_user ) <EOL> def test_invite_register ( self ) : <EOL> self . user_service . register ( "<STR_LIT>" , "<STR_LIT>" , self . plain_password , <EOL> True , invited_by = self . brl_user ) <EOL> saved_user = self . store . read_user ( "<STR_LIT>" ) <EOL> self . assertEquals ( saved_user . invited_by , self . brl_user ) <EOL> self . assertRaises ( ControledErrorException , self . user_service . register , <EOL> "<STR_LIT>" , "<STR_LIT>" , self . plain_password , <EOL> True , invited_by = "<STR_LIT>" ) <EOL> self . user_service . register ( "<STR_LIT>" , "<STR_LIT>" , self . plain_password , <EOL> True , invited_by = self . email ) <EOL> saved_user = self . store . read_user ( "<STR_LIT>" ) <EOL> self . assertEquals ( saved_user . invited_by , self . brl_user ) <EOL> self . assertRaises ( ControledErrorException , self . user_service . register , <EOL> "<STR_LIT>" , "<STR_LIT>" , self . plain_password , <EOL> True , invited_by = "<STR_LIT>" ) <EOL> def test_invalid_token ( self ) : <EOL> '''<STR_LIT>''' <EOL> brl_user , token_1 = self . user_service . authenticate ( self . brl_user , self . plain_password ) <EOL> manager = JWTCredentialsManagerFactory . new ( self . store ) <EOL> brl_user_test = manager . get_user ( token_1 ) <EOL> self . assertEqual ( brl_user , brl_user_test ) <EOL> sleep ( <NUM_LIT:0.1> ) <EOL> self . user_service . change_password ( self . brl_user , self . plain_password , "<STR_LIT>" ) <EOL> self . assertRaises ( DecodeError , manager . get_user , token_1 ) <EOL> @ patch ( '<STR_LIT>' ) <EOL> def test_oauth_register ( self , get_oauth_service ) : <EOL> email = "<STR_LIT>" <EOL> _ , brl_user = self . store . generate_non_persistent_user ( ) <EOL> oauth_service = Mock ( ) <EOL> oauth_service . get_user_info = Mock ( return_value = ( brl_user , email ) ) <EOL> get_oauth_service . return_value = oauth_service <EOL> self . bii_user = self . user_service . register ( brl_user , email , <EOL> self . plain_password , True , <EOL> "<STR_LIT>" , "<STR_LIT>" ) <EOL> saved_user = self . store . read_user ( brl_user ) <EOL> self . assertEqual ( saved_user . oauth_google_token , "<STR_LIT>" ) <EOL> self . assertIsNone ( saved_user . oauth_github_token ) <EOL> self . assertEqual ( saved_user . active , True ) <EOL> email = "<STR_LIT>" <EOL> _ , brl_user = self . store . generate_non_persistent_user ( ) <EOL> oauth_service = Mock ( ) <EOL> oauth_service . get_user_info = Mock ( return_value = ( brl_user , email ) ) <EOL> get_oauth_service . return_value = oauth_service <EOL> self . bii_user = self . user_service . register ( brl_user , email , <EOL> self . plain_password , True , <EOL> "<STR_LIT>" , "<STR_LIT>" ) <EOL> saved_user = self . store . read_user ( brl_user ) <EOL> self . assertEqual ( saved_user . oauth_github_token , "<STR_LIT>" ) <EOL> self . assertIsNone ( saved_user . oauth_google_token ) <EOL> self . assertEqual ( saved_user . active , True ) <EOL> email = "<STR_LIT>" <EOL> _ , brl_user = self . store . generate_non_persistent_user ( ) <EOL> oauth_service = Mock ( ) <EOL> oauth_service . get_user_info = Mock ( return_value = ( brl_user , "<STR_LIT>" ) ) <EOL> get_oauth_service . return_value = oauth_service <EOL> self . bii_user = self . user_service . register ( brl_user , email , <EOL> self . plain_password , True , <EOL> "<STR_LIT>" , "<STR_LIT>" ) <EOL> saved_user = self . store . read_user ( brl_user ) <EOL> self . assertEqual ( saved_user . oauth_github_token , "<STR_LIT>" ) <EOL> self . assertIsNone ( saved_user . oauth_google_token ) <EOL> self . assertEqual ( saved_user . active , False ) <EOL> email = "<STR_LIT>" <EOL> _ , brl_user = self . store . generate_non_persistent_user ( ) <EOL> oauth_service = Mock ( ) <EOL> oauth_service . get_user_info = Mock ( return_value = None ) <EOL> get_oauth_service . return_value = oauth_service <EOL> self . bii_user = self . user_service . register ( brl_user , email , <EOL> self . plain_password , True , <EOL> "<STR_LIT>" , "<STR_LIT>" ) <EOL> saved_user = self . store . read_user ( brl_user ) <EOL> self . assertIsNone ( saved_user . oauth_github_token ) <EOL> self . assertIsNone ( saved_user . oauth_google_token ) <EOL> self . assertEqual ( saved_user . active , False ) </s>
