<s> from nose . tools import * <EOL> from tests . base import ApiTestCase <EOL> from tests . factories import AuthUserFactory , InstitutionFactory <EOL> from api . base . settings . defaults import API_BASE <EOL> class TestUserInstititutionRelationship ( ApiTestCase ) : <EOL> def setUp ( self ) : <EOL> super ( TestUserInstititutionRelationship , self ) . setUp ( ) <EOL> self . user = AuthUserFactory ( ) <EOL> self . user2 = AuthUserFactory ( ) <EOL> self . url = '<STR_LIT>' . format ( API_BASE , self . user . _id ) <EOL> self . institution1 = InstitutionFactory ( ) <EOL> self . institution2 = InstitutionFactory ( ) <EOL> self . user . affiliated_institutions . append ( self . institution1 ) <EOL> self . user . affiliated_institutions . append ( self . institution2 ) <EOL> self . user . save ( ) <EOL> def test_get_relationship_institutions ( self ) : <EOL> res = self . app . get ( <EOL> self . url , auth = self . user . auth <EOL> ) <EOL> assert_equal ( res . status_code , <NUM_LIT:200> ) <EOL> assert_in ( self . user . absolute_api_v2_url + '<STR_LIT>' , res . json [ '<STR_LIT>' ] [ '<STR_LIT>' ] ) <EOL> assert_in ( self . user . absolute_api_v2_url + '<STR_LIT>' , res . json [ '<STR_LIT>' ] [ '<STR_LIT:html>' ] ) <EOL> ids = [ val [ '<STR_LIT:id>' ] for val in res . json [ '<STR_LIT:data>' ] ] <EOL> assert_in ( self . institution1 . _id , ids ) <EOL> assert_in ( self . institution2 . _id , ids ) <EOL> def test_get_institutions_relationship_while_logged_out ( self ) : <EOL> res = self . app . get ( <EOL> self . url <EOL> ) <EOL> ids = [ val [ '<STR_LIT:id>' ] for val in res . json [ '<STR_LIT:data>' ] ] <EOL> assert_in ( self . institution1 . _id , ids ) <EOL> assert_in ( self . institution2 . _id , ids ) <EOL> def test_post_with_auth ( self ) : <EOL> res = self . app . post_json_api ( <EOL> self . url , { } , <EOL> auth = self . user . auth , <EOL> expect_errors = True <EOL> ) <EOL> assert_equal ( res . status_code , <NUM_LIT> ) <EOL> def test_put_with_auth ( self ) : <EOL> res = self . app . put_json_api ( <EOL> self . url , { } , <EOL> auth = self . user . auth , <EOL> expect_errors = True <EOL> ) <EOL> assert_equal ( res . status_code , <NUM_LIT> ) <EOL> def test_post_without_auth ( self ) : <EOL> res = self . app . post_json_api ( <EOL> self . url , { } , expect_errors = True <EOL> ) <EOL> assert_equal ( res . status_code , <NUM_LIT> ) <EOL> def test_put_without_auth ( self ) : <EOL> res = self . app . put_json_api ( <EOL> self . url , { } , expect_errors = True <EOL> ) <EOL> assert_equal ( res . status_code , <NUM_LIT> ) <EOL> def test_delete_no_auth ( self ) : <EOL> res = self . app . delete_json_api ( <EOL> self . url , <EOL> { '<STR_LIT:data>' : [ <EOL> { '<STR_LIT:type>' : '<STR_LIT>' , '<STR_LIT:id>' : self . institution1 . _id } <EOL> ] } , <EOL> expect_errors = True <EOL> ) <EOL> assert_equal ( res . status_code , <NUM_LIT> ) <EOL> def test_delete_wrong_auth ( self ) : <EOL> res = self . app . delete_json_api ( <EOL> self . url , <EOL> { '<STR_LIT:data>' : [ <EOL> { '<STR_LIT:type>' : '<STR_LIT>' , '<STR_LIT:id>' : self . institution1 . _id } <EOL> ] } , <EOL> auth = self . user2 . auth , expect_errors = True <EOL> ) <EOL> assert_equal ( res . status_code , <NUM_LIT> ) <EOL> def test_delete_one ( self ) : <EOL> res = self . app . delete_json_api ( <EOL> self . url , <EOL> { '<STR_LIT:data>' : [ <EOL> { '<STR_LIT:type>' : '<STR_LIT>' , '<STR_LIT:id>' : self . institution1 . _id } <EOL> ] } , <EOL> auth = self . user . auth <EOL> ) <EOL> assert_equal ( res . status_code , <NUM_LIT> ) <EOL> self . user . reload ( ) <EOL> ids = [ inst . _id for inst in self . user . affiliated_institutions ] <EOL> assert_not_in ( self . institution1 . _id , ids ) <EOL> assert_in ( self . institution2 . _id , ids ) <EOL> def test_type_mistyped ( self ) : <EOL> res = self . app . delete_json_api ( <EOL> self . url , <EOL> { '<STR_LIT:data>' : [ <EOL> { '<STR_LIT:type>' : '<STR_LIT>' , '<STR_LIT:id>' : self . institution1 . _id } <EOL> ] } , <EOL> auth = self . user . auth , expect_errors = True <EOL> ) <EOL> assert_equal ( res . status_code , <NUM_LIT> ) <EOL> def test_delete_multiple ( self ) : <EOL> res = self . app . delete_json_api ( <EOL> self . url , <EOL> { '<STR_LIT:data>' : [ <EOL> { '<STR_LIT:type>' : '<STR_LIT>' , '<STR_LIT:id>' : self . institution1 . _id } , <EOL> { '<STR_LIT:type>' : '<STR_LIT>' , '<STR_LIT:id>' : self . institution2 . _id } <EOL> ] } , <EOL> auth = self . user . auth <EOL> ) <EOL> assert_equal ( res . status_code , <NUM_LIT> ) <EOL> self . user . reload ( ) <EOL> ids = [ inst . _id for inst in self . user . affiliated_institutions ] <EOL> assert_not_in ( self . institution1 . _id , ids ) <EOL> assert_not_in ( self . institution2 . _id , ids ) <EOL> def test_delete_one_not_existing ( self ) : <EOL> res = self . app . delete_json_api ( <EOL> self . url , <EOL> { '<STR_LIT:data>' : [ <EOL> { '<STR_LIT:type>' : '<STR_LIT>' , '<STR_LIT:id>' : '<STR_LIT>' } <EOL> ] } , <EOL> auth = self . user . auth <EOL> ) <EOL> assert_equal ( res . status_code , <NUM_LIT> ) <EOL> self . user . reload ( ) <EOL> ids = [ inst . _id for inst in self . user . affiliated_institutions ] <EOL> assert_in ( self . institution1 . _id , ids ) <EOL> assert_in ( self . institution2 . _id , ids ) <EOL> def test_attempt_payload_not_in_array ( self ) : <EOL> res = self . app . delete_json_api ( <EOL> self . url , <EOL> { '<STR_LIT:data>' : <EOL> { '<STR_LIT:type>' : '<STR_LIT>' , '<STR_LIT:id>' : self . institution1 . _id } <EOL> } , <EOL> auth = self . user . auth , expect_errors = True <EOL> ) <EOL> assert_equal ( res . status_code , <NUM_LIT> ) <EOL> def test_attempt_with_no_type_field ( self ) : <EOL> res = self . app . delete_json_api ( <EOL> self . url , <EOL> { '<STR_LIT:data>' : [ <EOL> { '<STR_LIT:id>' : self . institution1 . _id } <EOL> ] } , <EOL> auth = self . user . auth , expect_errors = True <EOL> ) <EOL> assert_equal ( res . status_code , <NUM_LIT> ) <EOL> def test_attempt_with_no_id_field ( self ) : <EOL> res = self . app . delete_json_api ( <EOL> self . url , <EOL> { '<STR_LIT:data>' : [ <EOL> { '<STR_LIT:type>' : '<STR_LIT>' } <EOL> ] } , <EOL> auth = self . user . auth , expect_errors = True <EOL> ) <EOL> assert_equal ( res . status_code , <NUM_LIT> ) </s>
