<s> from nose . tools import * <EOL> import json <EOL> import jwt <EOL> import jwe <EOL> from modularodm import Q <EOL> from tests . base import ApiTestCase <EOL> from tests . base import capture_signals <EOL> from tests . factories import InstitutionFactory <EOL> from api . base import settings <EOL> from api . base . settings . defaults import API_BASE <EOL> from framework . auth import signals <EOL> from website . models import User <EOL> class TestInstitutionAuth ( ApiTestCase ) : <EOL> def setUp ( self ) : <EOL> super ( TestInstitutionAuth , self ) . setUp ( ) <EOL> self . institution = InstitutionFactory ( ) <EOL> self . institution . save ( ) <EOL> self . url = '<STR_LIT>' . format ( API_BASE ) <EOL> def tearDown ( self ) : <EOL> super ( TestInstitutionAuth , self ) . tearDown ( ) <EOL> self . institution . remove ( ) <EOL> User . remove ( ) <EOL> def build_payload ( self , username ) : <EOL> data = { <EOL> '<STR_LIT>' : { <EOL> '<STR_LIT:id>' : self . institution . _id , <EOL> '<STR_LIT:user>' : { <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT:username>' : username <EOL> } <EOL> } <EOL> } <EOL> return jwe . encrypt ( jwt . encode ( { <EOL> '<STR_LIT>' : username , <EOL> '<STR_LIT:data>' : json . dumps ( data ) <EOL> } , settings . JWT_SECRET , algorithm = '<STR_LIT>' ) , settings . JWE_SECRET ) <EOL> def test_creates_user ( self ) : <EOL> username = '<STR_LIT>' <EOL> assert_equal ( User . find ( Q ( '<STR_LIT:username>' , '<STR_LIT>' , username ) ) . count ( ) , <NUM_LIT:0> ) <EOL> with capture_signals ( ) as mock_signals : <EOL> res = self . app . post ( self . url , self . build_payload ( username ) ) <EOL> assert_equal ( res . status_code , <NUM_LIT> ) <EOL> assert_equal ( mock_signals . signals_sent ( ) , set ( [ signals . user_confirmed ] ) ) <EOL> user = User . find_one ( Q ( '<STR_LIT:username>' , '<STR_LIT>' , username ) ) <EOL> assert_true ( user ) <EOL> assert_in ( self . institution , user . affiliated_institutions ) <EOL> def test_adds_institution ( self ) : <EOL> username = '<STR_LIT>' <EOL> user = User ( username = username , fullname = '<STR_LIT>' ) <EOL> user . save ( ) <EOL> with capture_signals ( ) as mock_signals : <EOL> res = self . app . post ( self . url , self . build_payload ( username ) ) <EOL> assert_equal ( res . status_code , <NUM_LIT> ) <EOL> assert_equal ( mock_signals . signals_sent ( ) , set ( ) ) <EOL> user . reload ( ) <EOL> assert_in ( self . institution , user . affiliated_institutions ) <EOL> def test_finds_user ( self ) : <EOL> username = '<STR_LIT>' <EOL> user = User ( username = username , fullname = '<STR_LIT>' ) <EOL> user . affiliated_institutions . append ( self . institution ) <EOL> user . save ( ) <EOL> res = self . app . post ( self . url , self . build_payload ( username ) ) <EOL> assert_equal ( res . status_code , <NUM_LIT> ) <EOL> user . reload ( ) <EOL> assert_equal ( len ( user . affiliated_institutions ) , <NUM_LIT:1> ) <EOL> def test_bad_token ( self ) : <EOL> res = self . app . post ( self . url , '<STR_LIT>' , expect_errors = True ) <EOL> assert_equal ( res . status_code , <NUM_LIT> ) </s>
