<s> import mock <EOL> import datetime <EOL> from modularodm import Q <EOL> from nose . tools import * <EOL> from framework import auth <EOL> from framework . auth import exceptions <EOL> from framework . exceptions import PermissionsError <EOL> from website import models , project <EOL> from tests import base <EOL> from tests . base import fake <EOL> from tests import factories <EOL> from framework . celery_tasks import handlers <EOL> class TestUser ( base . OsfTestCase ) : <EOL> def setUp ( self ) : <EOL> super ( TestUser , self ) . setUp ( ) <EOL> self . user = factories . AuthUserFactory ( ) <EOL> def tearDown ( self ) : <EOL> models . Node . remove ( ) <EOL> models . User . remove ( ) <EOL> models . Session . remove ( ) <EOL> super ( TestUser , self ) . tearDown ( ) <EOL> def test_add_unconfirmed_email_when_email_verifications_is_None ( self ) : <EOL> self . user . email_verifications = None <EOL> self . user . save ( ) <EOL> email = fake . email ( ) <EOL> self . user . add_unconfirmed_email ( email ) <EOL> self . user . save ( ) <EOL> assert_in ( email , self . user . unconfirmed_emails ) <EOL> def test_unconfirmed_emails ( self ) : <EOL> assert_equal ( <EOL> self . user . unconfirmed_emails , <EOL> [ ] <EOL> ) <EOL> self . user . add_unconfirmed_email ( '<STR_LIT>' ) <EOL> assert_equal ( <EOL> self . user . unconfirmed_emails , <EOL> [ '<STR_LIT>' ] <EOL> ) <EOL> self . user . email_verifications = None <EOL> self . user . save ( ) <EOL> assert_equal ( self . user . unconfirmed_emails , [ ] ) <EOL> def test_unconfirmed_emails_unregistered_user ( self ) : <EOL> assert_equal ( <EOL> factories . UnregUserFactory ( ) . unconfirmed_emails , <EOL> [ ] <EOL> ) <EOL> def test_unconfirmed_emails_unconfirmed_user ( self ) : <EOL> user = factories . UnconfirmedUserFactory ( ) <EOL> assert_equal ( <EOL> user . unconfirmed_emails , <EOL> [ user . username ] <EOL> ) <EOL> def test_remove_unconfirmed_email ( self ) : <EOL> self . user . add_unconfirmed_email ( '<STR_LIT>' ) <EOL> self . user . save ( ) <EOL> assert_in ( '<STR_LIT>' , self . user . unconfirmed_emails ) <EOL> self . user . remove_unconfirmed_email ( '<STR_LIT>' ) <EOL> self . user . save ( ) <EOL> assert_not_in ( '<STR_LIT>' , self . user . unconfirmed_emails ) <EOL> def test_confirm_email ( self ) : <EOL> token = self . user . add_unconfirmed_email ( '<STR_LIT>' ) <EOL> self . user . confirm_email ( token ) <EOL> assert_not_in ( '<STR_LIT>' , self . user . unconfirmed_emails ) <EOL> assert_in ( '<STR_LIT>' , self . user . emails ) <EOL> def test_confirm_email_comparison_is_case_insensitive ( self ) : <EOL> u = factories . UnconfirmedUserFactory . build ( <EOL> username = '<STR_LIT>' <EOL> ) <EOL> u . add_unconfirmed_email ( '<STR_LIT>' ) <EOL> u . save ( ) <EOL> assert_false ( u . is_confirmed ) <EOL> token = u . get_confirmation_token ( '<STR_LIT>' ) <EOL> confirmed = u . confirm_email ( token ) <EOL> assert_true ( confirmed ) <EOL> assert_true ( u . is_confirmed ) <EOL> def test_cannot_remove_primary_email_from_email_list ( self ) : <EOL> with assert_raises ( PermissionsError ) as e : <EOL> self . user . remove_email ( self . user . username ) <EOL> assert_equal ( e . exception . message , "<STR_LIT>" ) <EOL> def test_add_same_unconfirmed_email_twice ( self ) : <EOL> email = "<STR_LIT>" <EOL> token1 = self . user . add_unconfirmed_email ( email ) <EOL> self . user . save ( ) <EOL> self . user . reload ( ) <EOL> assert_equal ( token1 , self . user . get_confirmation_token ( email ) ) <EOL> assert_equal ( email , self . user . _get_unconfirmed_email_for_token ( token1 ) ) <EOL> token2 = self . user . add_unconfirmed_email ( email ) <EOL> self . user . save ( ) <EOL> self . user . reload ( ) <EOL> assert_not_equal ( token1 , self . user . get_confirmation_token ( email ) ) <EOL> assert_equal ( token2 , self . user . get_confirmation_token ( email ) ) <EOL> assert_equal ( email , self . user . _get_unconfirmed_email_for_token ( token2 ) ) <EOL> with assert_raises ( exceptions . InvalidTokenError ) : <EOL> self . user . _get_unconfirmed_email_for_token ( token1 ) <EOL> def test_contributed_property ( self ) : <EOL> projects_contributed_to = project . model . Node . find ( Q ( '<STR_LIT>' , '<STR_LIT>' , self . user . _id ) ) <EOL> assert_equal ( list ( self . user . contributed ) , list ( projects_contributed_to ) ) <EOL> def test_contributor_to_property ( self ) : <EOL> normal_node = factories . ProjectFactory ( creator = self . user ) <EOL> normal_contributed_node = factories . ProjectFactory ( ) <EOL> normal_contributed_node . add_contributor ( self . user ) <EOL> normal_contributed_node . save ( ) <EOL> deleted_node = factories . ProjectFactory ( creator = self . user , is_deleted = True ) <EOL> bookmark_collection_node = factories . BookmarkCollectionFactory ( creator = self . user ) <EOL> collection_node = factories . CollectionFactory ( creator = self . user ) <EOL> project_to_be_invisible_on = factories . ProjectFactory ( ) <EOL> project_to_be_invisible_on . add_contributor ( self . user , visible = False ) <EOL> project_to_be_invisible_on . save ( ) <EOL> contributor_to_nodes = [ node . _id for node in self . user . contributor_to ] <EOL> assert_in ( normal_node . _id , contributor_to_nodes ) <EOL> assert_in ( normal_contributed_node . _id , contributor_to_nodes ) <EOL> assert_in ( project_to_be_invisible_on . _id , contributor_to_nodes ) <EOL> assert_not_in ( deleted_node . _id , contributor_to_nodes ) <EOL> assert_not_in ( bookmark_collection_node . _id , contributor_to_nodes ) <EOL> assert_not_in ( collection_node . _id , contributor_to_nodes ) <EOL> def test_visible_contributor_to_property ( self ) : <EOL> invisible_contributor = factories . UserFactory ( ) <EOL> normal_node = factories . ProjectFactory ( creator = invisible_contributor ) <EOL> deleted_node = factories . ProjectFactory ( creator = invisible_contributor , is_deleted = True ) <EOL> bookmark_collection_node = factories . BookmarkCollectionFactory ( creator = invisible_contributor ) <EOL> collection_node = factories . CollectionFactory ( creator = invisible_contributor ) <EOL> project_to_be_invisible_on = factories . ProjectFactory ( ) <EOL> project_to_be_invisible_on . add_contributor ( invisible_contributor , visible = False ) <EOL> project_to_be_invisible_on . save ( ) <EOL> visible_contributor_to_nodes = [ node . _id for node in invisible_contributor . visible_contributor_to ] <EOL> assert_in ( normal_node . _id , visible_contributor_to_nodes ) <EOL> assert_not_in ( deleted_node . _id , visible_contributor_to_nodes ) <EOL> assert_not_in ( bookmark_collection_node . _id , visible_contributor_to_nodes ) <EOL> assert_not_in ( collection_node . _id , visible_contributor_to_nodes ) <EOL> assert_not_in ( project_to_be_invisible_on . _id , visible_contributor_to_nodes ) <EOL> def test_created_property ( self ) : <EOL> factories . ProjectFactory ( creator = self . user ) <EOL> projects_created_by_user = project . model . Node . find ( Q ( '<STR_LIT>' , '<STR_LIT>' , self . user . _id ) ) <EOL> assert_equal ( list ( self . user . created ) , list ( projects_created_by_user ) ) <EOL> class TestUserMerging ( base . OsfTestCase ) : <EOL> ADDONS_UNDER_TEST = { <EOL> '<STR_LIT>' : { <EOL> '<STR_LIT>' : factories . MockAddonUserSettings , <EOL> '<STR_LIT>' : factories . MockAddonNodeSettings , <EOL> } , <EOL> '<STR_LIT>' : { <EOL> '<STR_LIT>' : factories . MockAddonUserSettings , <EOL> '<STR_LIT>' : factories . MockAddonNodeSettings , <EOL> } , <EOL> '<STR_LIT>' : { <EOL> '<STR_LIT>' : factories . MockAddonUserSettingsMergeable , <EOL> '<STR_LIT>' : factories . MockAddonNodeSettings , <EOL> } <EOL> } <EOL> def setUp ( self ) : <EOL> super ( TestUserMerging , self ) . setUp ( ) <EOL> self . user = factories . UserFactory ( ) <EOL> with self . context : <EOL> handlers . celery_before_request ( ) <EOL> def _add_unconfirmed_user ( self ) : <EOL> self . unconfirmed = factories . UnconfirmedUserFactory ( ) <EOL> self . user . system_tags = [ '<STR_LIT>' , '<STR_LIT:user>' ] <EOL> self . unconfirmed . system_tags = [ '<STR_LIT>' , '<STR_LIT>' ] <EOL> def _add_unregistered_user ( self ) : <EOL> self . unregistered = factories . UnregUserFactory ( ) <EOL> self . project_with_unreg_contrib = factories . ProjectFactory ( ) <EOL> self . project_with_unreg_contrib . add_unregistered_contributor ( <EOL> fullname = '<STR_LIT>' , <EOL> email = self . unregistered . username , <EOL> auth = auth . Auth ( self . project_with_unreg_contrib . creator ) <EOL> ) <EOL> self . project_with_unreg_contrib . save ( ) <EOL> def test_can_be_merged_no_addons ( self ) : <EOL> assert_true ( self . user . can_be_merged ) <EOL> def test_can_be_merged_unmergable_addon ( self ) : <EOL> self . user . add_addon ( '<STR_LIT>' ) <EOL> assert_false ( self . user . can_be_merged ) <EOL> def test_can_be_merged_mergable_addon ( self ) : <EOL> self . user . add_addon ( '<STR_LIT>' ) <EOL> assert_true ( self . user . can_be_merged ) <EOL> def test_can_be_merged_both_addons ( self ) : <EOL> self . user . add_addon ( '<STR_LIT>' ) <EOL> self . user . add_addon ( '<STR_LIT>' ) <EOL> assert_false ( self . user . can_be_merged ) <EOL> def test_can_be_merged_delete_unmergable_addon ( self ) : <EOL> self . user . add_addon ( '<STR_LIT>' ) <EOL> self . user . add_addon ( '<STR_LIT>' ) <EOL> self . user . delete_addon ( '<STR_LIT>' ) <EOL> assert_true ( self . user . can_be_merged ) <EOL> def test_merge_unconfirmed_into_unmergeable ( self ) : <EOL> self . user . add_addon ( '<STR_LIT>' ) <EOL> self . user . save ( ) <EOL> assert_false ( self . user . can_be_merged ) <EOL> unconf = factories . UnconfirmedUserFactory ( ) <EOL> self . user . merge_user ( unconf ) <EOL> unreg = factories . UnregUserFactory ( ) <EOL> self . user . merge_user ( unreg ) <EOL> def test_merge_unmergeable_into_mergeable ( self ) : <EOL> unconfirmed = factories . UnconfirmedUserFactory ( ) <EOL> unconfirmed . add_addon ( '<STR_LIT>' ) <EOL> with assert_raises ( exceptions . MergeConflictError ) : <EOL> self . user . merge_user ( unconfirmed ) <EOL> unregistered = factories . UnregUserFactory ( ) <EOL> unregistered . add_addon ( '<STR_LIT>' ) <EOL> with assert_raises ( exceptions . MergeConflictError ) : <EOL> self . user . merge_user ( unregistered ) <EOL> def test_merge_unmergeabled_into_unmergeable ( self ) : <EOL> self . user . add_addon ( '<STR_LIT>' ) <EOL> unconfirmed = factories . UnconfirmedUserFactory ( ) <EOL> unconfirmed . add_addon ( '<STR_LIT>' ) <EOL> with assert_raises ( exceptions . MergeConflictError ) : <EOL> self . user . merge_user ( unconfirmed ) <EOL> unregistered = factories . UnregUserFactory ( ) <EOL> unregistered . add_addon ( '<STR_LIT>' ) <EOL> with assert_raises ( exceptions . MergeConflictError ) : <EOL> self . user . merge_user ( unregistered ) <EOL> @ mock . patch ( '<STR_LIT>' ) <EOL> def test_merge ( self , mock_get_mailchimp_api ) : <EOL> other_user = factories . UserFactory ( ) <EOL> other_user . save ( ) <EOL> today = datetime . datetime . now ( ) <EOL> yesterday = today - datetime . timedelta ( days = <NUM_LIT:1> ) <EOL> self . user . comments_viewed_timestamp [ '<STR_LIT>' ] = today <EOL> other_user . comments_viewed_timestamp [ '<STR_LIT>' ] = yesterday <EOL> self . user . comments_viewed_timestamp [ '<STR_LIT>' ] = yesterday <EOL> other_user . comments_viewed_timestamp [ '<STR_LIT>' ] = today <EOL> self . user . comments_viewed_timestamp [ '<STR_LIT:user>' ] = yesterday <EOL> other_user . comments_viewed_timestamp [ '<STR_LIT>' ] = yesterday <EOL> self . user . email_verifications = { '<STR_LIT:user>' : { '<STR_LIT:email>' : '<STR_LIT:a>' } } <EOL> other_user . email_verifications = { '<STR_LIT>' : { '<STR_LIT:email>' : '<STR_LIT:b>' } } <EOL> self . user . external_accounts = [ factories . ExternalAccountFactory ( ) ] <EOL> other_user . external_accounts = [ factories . ExternalAccountFactory ( ) ] <EOL> self . user . mailchimp_mailing_lists = { <EOL> '<STR_LIT:user>' : True , <EOL> '<STR_LIT>' : True , <EOL> '<STR_LIT>' : False , <EOL> } <EOL> other_user . mailchimp_mailing_lists = { <EOL> '<STR_LIT>' : True , <EOL> '<STR_LIT>' : False , <EOL> '<STR_LIT>' : True , <EOL> } <EOL> self . user . piwik_token = '<STR_LIT:abc>' <EOL> other_user . piwik_token = '<STR_LIT>' <EOL> self . user . security_messages = { <EOL> '<STR_LIT:user>' : today , <EOL> '<STR_LIT>' : today , <EOL> } <EOL> other_user . security_messages = { <EOL> '<STR_LIT>' : today , <EOL> '<STR_LIT>' : today , <EOL> } <EOL> self . user . system_tags = [ '<STR_LIT:user>' , '<STR_LIT>' ] <EOL> other_user . system_tags = [ '<STR_LIT>' , '<STR_LIT>' ] <EOL> self . user . watched = [ factories . WatchConfigFactory ( ) ] <EOL> other_user . watched = [ factories . WatchConfigFactory ( ) ] <EOL> self . user . save ( ) <EOL> other_user . save ( ) <EOL> default_to_master_user_fields = [ <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT:password>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT:username>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' <EOL> ] <EOL> calculated_fields = { <EOL> '<STR_LIT>' : { <EOL> '<STR_LIT:user>' : yesterday , <EOL> '<STR_LIT>' : yesterday , <EOL> '<STR_LIT>' : today , <EOL> '<STR_LIT>' : today , <EOL> } , <EOL> '<STR_LIT>' : { <EOL> '<STR_LIT:user>' : { '<STR_LIT:email>' : '<STR_LIT:a>' } , <EOL> '<STR_LIT>' : { '<STR_LIT:email>' : '<STR_LIT:b>' } , <EOL> } , <EOL> '<STR_LIT>' : [ <EOL> self . user . username , <EOL> other_user . username , <EOL> ] , <EOL> '<STR_LIT>' : [ <EOL> self . user . external_accounts [ <NUM_LIT:0> ] . _id , <EOL> other_user . external_accounts [ <NUM_LIT:0> ] . _id , <EOL> ] , <EOL> '<STR_LIT>' : { <EOL> '<STR_LIT:user>' : True , <EOL> '<STR_LIT>' : True , <EOL> '<STR_LIT>' : True , <EOL> '<STR_LIT>' : True , <EOL> } , <EOL> '<STR_LIT>' : { <EOL> '<STR_LIT>' : True <EOL> } , <EOL> '<STR_LIT>' : { <EOL> '<STR_LIT:user>' : today , <EOL> '<STR_LIT>' : today , <EOL> '<STR_LIT>' : today , <EOL> } , <EOL> '<STR_LIT>' : [ '<STR_LIT:user>' , '<STR_LIT>' , '<STR_LIT>' ] , <EOL> '<STR_LIT>' : { } , <EOL> '<STR_LIT>' : [ <EOL> self . user . watched [ <NUM_LIT:0> ] . _id , <EOL> other_user . watched [ <NUM_LIT:0> ] . _id , <EOL> ] , <EOL> } <EOL> expected = { } <EOL> expected . update ( calculated_fields ) <EOL> for key in default_to_master_user_fields : <EOL> expected [ key ] = getattr ( self . user , key ) <EOL> assert_equal ( <EOL> set ( expected . keys ( ) ) , <EOL> set ( self . user . _fields ) , <EOL> ) <EOL> mock_client = mock . MagicMock ( ) <EOL> mock_get_mailchimp_api . return_value = mock_client <EOL> mock_client . lists . list . return_value = { '<STR_LIT:data>' : [ { '<STR_LIT:id>' : x , '<STR_LIT>' : list_name } for x , list_name in enumerate ( self . user . mailchimp_mailing_lists ) ] } <EOL> self . user . merge_user ( other_user ) <EOL> self . user . save ( ) <EOL> handlers . celery_teardown_request ( ) <EOL> for k , v in expected . iteritems ( ) : <EOL> assert_equal ( <EOL> getattr ( self . user , k ) , <EOL> v , <EOL> ) <EOL> assert_equal ( other_user . merged_by , self . user ) <EOL> assert_equal ( <EOL> <NUM_LIT:0> , <EOL> models . Session . find ( <EOL> Q ( '<STR_LIT>' , '<STR_LIT>' , other_user . _id ) <EOL> ) . count ( ) <EOL> ) <EOL> def test_merge_unconfirmed ( self ) : <EOL> self . _add_unconfirmed_user ( ) <EOL> unconfirmed_username = self . unconfirmed . username <EOL> self . user . merge_user ( self . unconfirmed ) <EOL> assert_true ( self . unconfirmed . is_merged ) <EOL> assert_equal ( self . unconfirmed . merged_by , self . user ) <EOL> assert_true ( self . user . is_claimed ) <EOL> assert_false ( self . user . is_invited ) <EOL> assert_equal ( self . user . system_tags , [ '<STR_LIT>' , '<STR_LIT:user>' , '<STR_LIT>' ] ) <EOL> assert_equal ( self . unconfirmed . email_verifications , { } ) <EOL> assert_is_none ( self . unconfirmed . username ) <EOL> assert_is_none ( self . unconfirmed . password ) <EOL> assert_is_none ( self . unconfirmed . verification_key ) <EOL> unconfirmed_emails = [ record [ '<STR_LIT:email>' ] for record in self . user . email_verifications . values ( ) ] <EOL> assert_not_in ( unconfirmed_username , unconfirmed_emails ) <EOL> def test_merge_unregistered ( self ) : <EOL> self . _add_unregistered_user ( ) <EOL> self . user . merge_user ( self . unregistered ) <EOL> self . project_with_unreg_contrib . reload ( ) <EOL> assert_true ( self . user . is_invited ) <EOL> assert_in ( self . user , self . project_with_unreg_contrib . contributors ) <EOL> @ mock . patch ( '<STR_LIT>' ) <EOL> def test_merge_doesnt_send_signal ( self , mock_notify ) : <EOL> project . signals . contributor_added . connect ( project . views . contributor . notify_added_contributor ) <EOL> other_user = factories . UserFactory ( ) <EOL> self . user . merge_user ( other_user ) <EOL> assert_equal ( other_user . merged_by . _id , self . user . _id ) <EOL> assert_false ( mock_notify . called ) </s>
