<s> from nose . tools import * <EOL> from tests . factories import ( <EOL> ProjectFactory , <EOL> UserFactory , <EOL> RegistrationFactory , <EOL> NodeFactory , <EOL> NodeLogFactory , <EOL> CollectionFactory , <EOL> ) <EOL> from tests . base import OsfTestCase <EOL> from framework . auth import Auth <EOL> from framework import utils as framework_utils <EOL> from website . project . views . node import _get_summary , _view_project , _serialize_node_search , _get_children <EOL> from website . views import _render_node <EOL> from website . profile import utils <EOL> from website . views import serialize_log <EOL> from website . util import permissions <EOL> class TestNodeSerializers ( OsfTestCase ) : <EOL> def test_get_summary_private_node_should_include_id_and_primary_boolean_reg_and_fork ( self ) : <EOL> user = UserFactory ( ) <EOL> node = ProjectFactory ( is_public = False ) <EOL> result = _get_summary ( <EOL> node , auth = Auth ( user ) , <EOL> primary = True , <EOL> link_id = None <EOL> ) <EOL> assert_equal ( result [ '<STR_LIT>' ] [ '<STR_LIT:id>' ] , node . _primary_key ) <EOL> assert_true ( result [ '<STR_LIT>' ] [ '<STR_LIT>' ] , True ) <EOL> assert_equal ( result [ '<STR_LIT>' ] [ '<STR_LIT>' ] , node . is_registration ) <EOL> assert_equal ( result [ '<STR_LIT>' ] [ '<STR_LIT>' ] , node . is_fork ) <EOL> def test_get_summary_for_registration_uses_correct_date_format ( self ) : <EOL> reg = RegistrationFactory ( ) <EOL> res = _get_summary ( reg , auth = Auth ( reg . creator ) ) <EOL> assert_equal ( res [ '<STR_LIT>' ] [ '<STR_LIT>' ] , <EOL> reg . registered_date . strftime ( '<STR_LIT>' ) ) <EOL> def test_get_summary_private_registration_should_include_is_registration ( self ) : <EOL> user = UserFactory ( ) <EOL> node = ProjectFactory ( is_public = True ) <EOL> reg = RegistrationFactory ( project = node , user = node . creator ) <EOL> res = _get_summary ( reg , auth = Auth ( user ) ) <EOL> assert_true ( res [ '<STR_LIT>' ] [ '<STR_LIT>' ] ) <EOL> def test_render_node ( self ) : <EOL> node = ProjectFactory ( ) <EOL> res = _render_node ( node ) <EOL> assert_equal ( res [ '<STR_LIT:title>' ] , node . title ) <EOL> assert_equal ( res [ '<STR_LIT:id>' ] , node . _primary_key ) <EOL> assert_equal ( res [ '<STR_LIT:url>' ] , node . url ) <EOL> assert_equal ( res [ '<STR_LIT>' ] , node . api_url ) <EOL> assert_equal ( res [ '<STR_LIT>' ] , node . primary ) <EOL> assert_equal ( res [ '<STR_LIT>' ] , framework_utils . iso8601format ( node . date_modified ) ) <EOL> assert_equal ( res [ '<STR_LIT>' ] , '<STR_LIT>' ) <EOL> def test_render_node_returns_permissions ( self ) : <EOL> node = ProjectFactory ( ) <EOL> admin = UserFactory ( ) <EOL> node . add_contributor ( admin , auth = Auth ( node . creator ) , <EOL> permissions = permissions . expand_permissions ( permissions . ADMIN ) ) <EOL> writer = UserFactory ( ) <EOL> node . add_contributor ( writer , auth = Auth ( node . creator ) , <EOL> permissions = permissions . expand_permissions ( permissions . WRITE ) ) <EOL> node . save ( ) <EOL> res_admin = _render_node ( node , Auth ( admin ) ) <EOL> assert_equal ( res_admin [ '<STR_LIT>' ] , '<STR_LIT>' ) <EOL> res_writer = _render_node ( node , Auth ( writer ) ) <EOL> assert_equal ( res_writer [ '<STR_LIT>' ] , '<STR_LIT>' ) <EOL> def test_get_children_only_returns_child_nodes_with_admin_permissions ( self ) : <EOL> user = UserFactory ( ) <EOL> admin_project = ProjectFactory ( ) <EOL> admin_project . add_contributor ( user , auth = Auth ( admin_project . creator ) , <EOL> permissions = permissions . expand_permissions ( permissions . ADMIN ) ) <EOL> admin_project . save ( ) <EOL> admin_component = NodeFactory ( parent = admin_project ) <EOL> admin_component . add_contributor ( user , auth = Auth ( admin_component . creator ) , <EOL> permissions = permissions . expand_permissions ( permissions . ADMIN ) ) <EOL> admin_component . save ( ) <EOL> read_and_write = NodeFactory ( parent = admin_project ) <EOL> read_and_write . add_contributor ( user , auth = Auth ( read_and_write . creator ) , <EOL> permissions = permissions . expand_permissions ( permissions . WRITE ) ) <EOL> read_and_write . save ( ) <EOL> read_only = NodeFactory ( parent = admin_project ) <EOL> read_only . add_contributor ( user , auth = Auth ( read_only . creator ) , <EOL> permissions = permissions . expand_permissions ( permissions . READ ) ) <EOL> read_only . save ( ) <EOL> non_contributor = NodeFactory ( parent = admin_project ) <EOL> components = _get_children ( admin_project , Auth ( user ) ) <EOL> assert_equal ( len ( components ) , <NUM_LIT:1> ) <EOL> def test_get_summary_private_fork_should_include_is_fork ( self ) : <EOL> user = UserFactory ( ) <EOL> node = ProjectFactory ( is_public = True ) <EOL> consolidated_auth = Auth ( user = node . creator ) <EOL> fork = node . fork_node ( consolidated_auth ) <EOL> res = _get_summary ( <EOL> fork , auth = Auth ( user ) , <EOL> primary = True , <EOL> link_id = None <EOL> ) <EOL> assert_true ( res [ '<STR_LIT>' ] [ '<STR_LIT>' ] ) <EOL> def test_get_summary_private_fork_private_project_should_include_is_fork ( self ) : <EOL> user = UserFactory ( ) <EOL> node = ProjectFactory ( is_public = False ) <EOL> node . add_contributor ( user ) <EOL> consolidated_auth = Auth ( user = node . creator ) <EOL> fork = node . fork_node ( consolidated_auth ) <EOL> res = _get_summary ( <EOL> fork , auth = Auth ( user ) , <EOL> primary = True , <EOL> link_id = None <EOL> ) <EOL> assert_false ( res [ '<STR_LIT>' ] [ '<STR_LIT>' ] ) <EOL> assert_true ( res [ '<STR_LIT>' ] [ '<STR_LIT>' ] ) <EOL> def test_serialize_node_search_returns_only_visible_contributors ( self ) : <EOL> node = NodeFactory ( ) <EOL> non_visible_contributor = UserFactory ( ) <EOL> node . add_contributor ( non_visible_contributor , visible = False ) <EOL> serialized_node = _serialize_node_search ( node ) <EOL> assert_equal ( serialized_node [ '<STR_LIT>' ] , node . visible_contributors [ <NUM_LIT:0> ] . family_name ) <EOL> assert_equal ( len ( node . visible_contributors ) , <NUM_LIT:1> ) <EOL> assert_false ( serialized_node [ '<STR_LIT>' ] ) <EOL> class TestViewProject ( OsfTestCase ) : <EOL> def setUp ( self ) : <EOL> super ( TestViewProject , self ) . setUp ( ) <EOL> self . user = UserFactory ( ) <EOL> self . node = ProjectFactory ( creator = self . user ) <EOL> def test_view_project_pointer_count_excludes_folders ( self ) : <EOL> pointer_project = ProjectFactory ( is_public = True ) <EOL> pointed_project = self . node <EOL> pointer_project . add_pointer ( pointed_project , Auth ( pointer_project . creator ) , save = True ) <EOL> folder = CollectionFactory ( creator = pointed_project . creator ) <EOL> folder . add_pointer ( pointed_project , Auth ( pointed_project . creator ) , save = True ) <EOL> result = _view_project ( pointed_project , Auth ( pointed_project . creator ) ) <EOL> assert_equal ( result [ '<STR_LIT>' ] [ '<STR_LIT>' ] , <NUM_LIT:1> ) <EOL> def test_view_project_pending_registration_for_admin_contributor_does_contain_cancel_link ( self ) : <EOL> pending_reg = RegistrationFactory ( project = self . node , archive = True ) <EOL> assert_true ( pending_reg . is_pending_registration ) <EOL> result = _view_project ( pending_reg , Auth ( self . user ) ) <EOL> assert_not_equal ( result [ '<STR_LIT>' ] [ '<STR_LIT>' ] , '<STR_LIT>' ) <EOL> assert_in ( '<STR_LIT>' , result [ '<STR_LIT>' ] [ '<STR_LIT>' ] ) <EOL> pending_reg . remove ( ) <EOL> def test_view_project_pending_registration_for_write_contributor_does_not_contain_cancel_link ( self ) : <EOL> write_user = UserFactory ( ) <EOL> self . node . add_contributor ( write_user , permissions = permissions . WRITE , <EOL> auth = Auth ( self . user ) , save = True ) <EOL> pending_reg = RegistrationFactory ( project = self . node , archive = True ) <EOL> assert_true ( pending_reg . is_pending_registration ) <EOL> result = _view_project ( pending_reg , Auth ( write_user ) ) <EOL> assert_equal ( result [ '<STR_LIT>' ] [ '<STR_LIT>' ] , '<STR_LIT>' ) <EOL> pending_reg . remove ( ) <EOL> class TestNodeLogSerializers ( OsfTestCase ) : <EOL> def test_serialize_log ( self ) : <EOL> node = NodeFactory ( category = '<STR_LIT>' ) <EOL> node . save ( ) <EOL> log = NodeLogFactory ( <EOL> params = { '<STR_LIT>' : node . _id } , <EOL> node = node , <EOL> original_node = node <EOL> ) <EOL> d = serialize_log ( log ) <EOL> assert_equal ( d [ '<STR_LIT:action>' ] , log . action ) <EOL> assert_equal ( d [ '<STR_LIT>' ] [ '<STR_LIT>' ] , '<STR_LIT>' ) <EOL> assert_equal ( d [ '<STR_LIT>' ] [ '<STR_LIT>' ] , '<STR_LIT>' ) <EOL> assert_equal ( d [ '<STR_LIT>' ] [ '<STR_LIT:url>' ] , log . node . url ) <EOL> assert_equal ( d [ '<STR_LIT:date>' ] , framework_utils . iso8601format ( log . date ) ) <EOL> assert_in ( '<STR_LIT>' , d ) <EOL> assert_equal ( d [ '<STR_LIT:user>' ] [ '<STR_LIT>' ] , log . user . fullname ) <EOL> assert_equal ( d [ '<STR_LIT:user>' ] [ '<STR_LIT:url>' ] , log . user . url ) <EOL> assert_equal ( d [ '<STR_LIT>' ] , log . params ) <EOL> assert_equal ( d [ '<STR_LIT>' ] [ '<STR_LIT:title>' ] , log . node . title ) <EOL> def test_serialize_node_for_logs ( self ) : <EOL> node = NodeFactory ( ) <EOL> d = node . serialize ( ) <EOL> assert_equal ( d [ '<STR_LIT:id>' ] , node . _primary_key ) <EOL> assert_equal ( d [ '<STR_LIT>' ] , node . category_display ) <EOL> assert_equal ( d [ '<STR_LIT>' ] , node . project_or_component ) <EOL> assert_equal ( d [ '<STR_LIT:url>' ] , node . url ) <EOL> assert_equal ( d [ '<STR_LIT:title>' ] , node . title ) <EOL> assert_equal ( d [ '<STR_LIT>' ] , node . api_url ) <EOL> assert_equal ( d [ '<STR_LIT>' ] , node . is_public ) <EOL> assert_equal ( d [ '<STR_LIT>' ] , node . is_registration ) <EOL> class TestAddContributorJson ( OsfTestCase ) : <EOL> def setUp ( self ) : <EOL> super ( TestAddContributorJson , self ) . setUp ( ) <EOL> self . user = UserFactory ( ) <EOL> self . profile = self . user . profile_url <EOL> self . user_id = self . user . _primary_key <EOL> self . fullname = self . user . fullname <EOL> self . username = self . user . username <EOL> self . jobs = [ { <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT:title>' : '<STR_LIT>' , <EOL> '<STR_LIT:start>' : None , <EOL> '<STR_LIT:end>' : None , <EOL> } ] <EOL> self . schools = [ { <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT:location>' : '<STR_LIT>' , <EOL> '<STR_LIT:start>' : None , <EOL> '<STR_LIT:end>' : None , <EOL> } ] <EOL> def test_add_contributor_json ( self ) : <EOL> user_info = utils . add_contributor_json ( self . user ) <EOL> assert_equal ( user_info [ '<STR_LIT>' ] , self . fullname ) <EOL> assert_equal ( user_info [ '<STR_LIT:email>' ] , self . username ) <EOL> assert_equal ( user_info [ '<STR_LIT:id>' ] , self . user_id ) <EOL> assert_equal ( user_info [ '<STR_LIT>' ] , None ) <EOL> assert_equal ( user_info [ '<STR_LIT>' ] , None ) <EOL> assert_equal ( user_info [ '<STR_LIT>' ] , <NUM_LIT:0> ) <EOL> assert_equal ( user_info [ '<STR_LIT>' ] , True ) <EOL> assert_equal ( user_info [ '<STR_LIT>' ] , True ) <EOL> assert_in ( '<STR_LIT>' , user_info [ '<STR_LIT>' ] ) <EOL> assert_equal ( user_info [ '<STR_LIT>' ] , self . profile ) <EOL> def test_add_contributor_json_with_edu ( self ) : <EOL> self . user . schools = self . schools <EOL> user_info = utils . add_contributor_json ( self . user ) <EOL> assert_equal ( user_info [ '<STR_LIT>' ] , self . fullname ) <EOL> assert_equal ( user_info [ '<STR_LIT:email>' ] , self . username ) <EOL> assert_equal ( user_info [ '<STR_LIT:id>' ] , self . user_id ) <EOL> assert_equal ( user_info [ '<STR_LIT>' ] , None ) <EOL> assert_equal ( user_info [ '<STR_LIT>' ] , self . user . schools [ <NUM_LIT:0> ] [ '<STR_LIT>' ] ) <EOL> assert_equal ( user_info [ '<STR_LIT>' ] , <NUM_LIT:0> ) <EOL> assert_equal ( user_info [ '<STR_LIT>' ] , True ) <EOL> assert_equal ( user_info [ '<STR_LIT>' ] , True ) <EOL> assert_in ( '<STR_LIT>' , user_info [ '<STR_LIT>' ] ) <EOL> assert_equal ( user_info [ '<STR_LIT>' ] , self . profile ) <EOL> def test_add_contributor_json_with_job ( self ) : <EOL> self . user . jobs = self . jobs <EOL> user_info = utils . add_contributor_json ( self . user ) <EOL> assert_equal ( user_info [ '<STR_LIT>' ] , self . fullname ) <EOL> assert_equal ( user_info [ '<STR_LIT:email>' ] , self . username ) <EOL> assert_equal ( user_info [ '<STR_LIT:id>' ] , self . user_id ) <EOL> assert_equal ( user_info [ '<STR_LIT>' ] , self . user . jobs [ <NUM_LIT:0> ] [ '<STR_LIT>' ] ) <EOL> assert_equal ( user_info [ '<STR_LIT>' ] , None ) <EOL> assert_equal ( user_info [ '<STR_LIT>' ] , <NUM_LIT:0> ) <EOL> assert_equal ( user_info [ '<STR_LIT>' ] , True ) <EOL> assert_equal ( user_info [ '<STR_LIT>' ] , True ) <EOL> assert_in ( '<STR_LIT>' , user_info [ '<STR_LIT>' ] ) <EOL> assert_equal ( user_info [ '<STR_LIT>' ] , self . profile ) <EOL> def test_add_contributor_json_with_job_and_edu ( self ) : <EOL> self . user . jobs = self . jobs <EOL> self . user . schools = self . schools <EOL> user_info = utils . add_contributor_json ( self . user ) <EOL> assert_equal ( user_info [ '<STR_LIT>' ] , self . fullname ) <EOL> assert_equal ( user_info [ '<STR_LIT:email>' ] , self . username ) <EOL> assert_equal ( user_info [ '<STR_LIT:id>' ] , self . user_id ) <EOL> assert_equal ( user_info [ '<STR_LIT>' ] , self . user . jobs [ <NUM_LIT:0> ] [ '<STR_LIT>' ] ) <EOL> assert_equal ( user_info [ '<STR_LIT>' ] , self . user . schools [ <NUM_LIT:0> ] [ '<STR_LIT>' ] ) <EOL> assert_equal ( user_info [ '<STR_LIT>' ] , <NUM_LIT:0> ) <EOL> assert_equal ( user_info [ '<STR_LIT>' ] , True ) <EOL> assert_equal ( user_info [ '<STR_LIT>' ] , True ) <EOL> assert_in ( '<STR_LIT>' , user_info [ '<STR_LIT>' ] ) <EOL> assert_equal ( user_info [ '<STR_LIT>' ] , self . profile ) </s>
