<s> from collections import namedtuple <EOL> from datetime import datetime , timedelta <EOL> from urllib import urlencode <EOL> import pytest <EOL> from fixtures import standard_graph , graph , users , groups , session , permissions <EOL> from fixtures import fe_app as app <EOL> from grouper . audit import ( <EOL> assert_can_join , assert_controllers_are_auditors , get_audits , user_is_auditor , <EOL> UserNotAuditor , <EOL> ) <EOL> from url_util import url <EOL> from util import add_member , grant_permission <EOL> from grouper . models . audit_log import AuditLogCategory , AuditLog <EOL> def test_group_audited ( standard_graph , session , groups , permissions ) : <EOL> """<STR_LIT>""" <EOL> graph = standard_graph <EOL> assert not graph . get_group_details ( "<STR_LIT>" ) [ "<STR_LIT>" ] <EOL> assert graph . get_group_details ( "<STR_LIT>" ) [ "<STR_LIT>" ] <EOL> assert graph . get_group_details ( "<STR_LIT>" ) [ "<STR_LIT>" ] <EOL> def test_user_is_auditor ( standard_graph ) : <EOL> """<STR_LIT>""" <EOL> assert user_is_auditor ( "<STR_LIT>" ) <EOL> assert not user_is_auditor ( "<STR_LIT>" ) <EOL> def test_assert_can_join ( users , groups ) : <EOL> """<STR_LIT>""" <EOL> assert assert_can_join ( groups [ "<STR_LIT>" ] , users [ "<STR_LIT>" ] , role = "<STR_LIT>" ) <EOL> assert assert_can_join ( groups [ "<STR_LIT>" ] , users [ "<STR_LIT>" ] , role = "<STR_LIT>" ) <EOL> with pytest . raises ( UserNotAuditor ) : <EOL> assert not assert_can_join ( groups [ "<STR_LIT>" ] , users [ "<STR_LIT>" ] , role = "<STR_LIT>" ) <EOL> assert assert_can_join ( groups [ "<STR_LIT>" ] , users [ "<STR_LIT>" ] ) <EOL> with pytest . raises ( UserNotAuditor ) : <EOL> assert not assert_can_join ( groups [ "<STR_LIT>" ] , groups [ "<STR_LIT>" ] ) <EOL> assert assert_can_join ( groups [ "<STR_LIT>" ] , groups [ "<STR_LIT>" ] ) <EOL> assert assert_can_join ( groups [ "<STR_LIT>" ] , groups [ "<STR_LIT>" ] ) <EOL> with pytest . raises ( UserNotAuditor ) : <EOL> assert not assert_can_join ( groups [ "<STR_LIT>" ] , groups [ "<STR_LIT>" ] ) <EOL> def test_assert_controllers_are_auditors ( groups ) : <EOL> """<STR_LIT>""" <EOL> assert assert_controllers_are_auditors ( groups [ "<STR_LIT>" ] ) <EOL> with pytest . raises ( UserNotAuditor ) : <EOL> assert not assert_controllers_are_auditors ( groups [ "<STR_LIT>" ] ) <EOL> @ pytest . mark . gen_test <EOL> def test_audit_end_to_end ( session , users , groups , http_client , base_url ) : <EOL> """<STR_LIT>""" <EOL> groupname = '<STR_LIT>' <EOL> zay_id = users [ "<STR_LIT>" ] . id <EOL> gary_id = users [ "<STR_LIT>" ] . id <EOL> add_member ( groups [ "<STR_LIT>" ] , users [ "<STR_LIT>" ] ) <EOL> add_member ( groups [ "<STR_LIT>" ] , users [ "<STR_LIT>" ] ) <EOL> add_member ( groups [ "<STR_LIT>" ] , users [ "<STR_LIT>" ] ) <EOL> add_member ( groups [ "<STR_LIT>" ] , users [ "<STR_LIT>" ] ) <EOL> add_member ( groups [ groupname ] , users [ "<STR_LIT>" ] ) <EOL> add_member ( groups [ groupname ] , users [ "<STR_LIT>" ] ) <EOL> end_at_str = ( datetime . now ( ) + timedelta ( days = <NUM_LIT:10> ) ) . strftime ( '<STR_LIT>' ) <EOL> fe_url = url ( base_url , '<STR_LIT>' ) <EOL> resp = yield http_client . fetch ( fe_url , method = "<STR_LIT:POST>" , <EOL> body = urlencode ( { '<STR_LIT>' : end_at_str } ) , headers = { '<STR_LIT>' : '<STR_LIT>' } ) <EOL> assert resp . code == <NUM_LIT:200> <EOL> open_audits = get_audits ( session , only_open = True ) . all ( ) <EOL> assert len ( open_audits ) == <NUM_LIT:4> , '<STR_LIT>' <EOL> assert groupname in [ x . group . name for x in open_audits ] , '<STR_LIT>' <EOL> AuditMember = namedtuple ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> Audit = namedtuple ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> all_group_ids = [ x . group . id for x in open_audits ] <EOL> open_audits = [ Audit ( x . id , x . group . my_owners ( ) . iterkeys ( ) . next ( ) , x . group . name , <EOL> [ AuditMember ( am . id , am . edge . member_type , am . edge_id ) for am in x . my_members ( ) ] ) for <EOL> x in open_audits ] <EOL> for one_audit in open_audits : <EOL> fe_url = url ( base_url , '<STR_LIT>' . format ( one_audit . audit_id ) ) <EOL> if one_audit . group_name == groupname : <EOL> continue <EOL> body = urlencode ( { "<STR_LIT>" . format ( am . am_id ) : "<STR_LIT>" for am in <EOL> one_audit . audit_members } ) <EOL> resp = yield http_client . fetch ( fe_url , method = "<STR_LIT:POST>" , body = body , <EOL> headers = { '<STR_LIT>' : one_audit . owner_name } ) <EOL> assert resp . code == <NUM_LIT:200> <EOL> open_audits = get_audits ( session , only_open = True ) . all ( ) <EOL> assert len ( open_audits ) == <NUM_LIT:1> , '<STR_LIT>' <EOL> one_audit = open_audits [ <NUM_LIT:0> ] <EOL> one_audit . id <EOL> body_dict = { } <EOL> for am in one_audit . my_members ( ) : <EOL> if gary_id == am . member . id : <EOL> body_dict [ "<STR_LIT>" . format ( am . id ) ] = "<STR_LIT>" <EOL> else : <EOL> body_dict [ "<STR_LIT>" . format ( am . id ) ] = "<STR_LIT>" <EOL> owner_name = one_audit . group . my_owners ( ) . iterkeys ( ) . next ( ) <EOL> fe_url = url ( base_url , '<STR_LIT>' . format ( one_audit . id ) ) <EOL> resp = yield http_client . fetch ( fe_url , method = "<STR_LIT:POST>" , body = urlencode ( body_dict ) , <EOL> headers = { '<STR_LIT>' : owner_name } ) <EOL> assert resp . code == <NUM_LIT:200> <EOL> assert len ( AuditLog . get_entries ( session , action = '<STR_LIT>' ) ) == <NUM_LIT:1> , '<STR_LIT>' <EOL> assert len ( AuditLog . get_entries ( session , <EOL> action = '<STR_LIT>' ) ) == <NUM_LIT:1> , '<STR_LIT>' <EOL> for group_id in all_group_ids : <EOL> assert len ( AuditLog . get_entries ( session , on_group_id = group_id , action = '<STR_LIT>' , <EOL> category = AuditLogCategory . audit ) ) == <NUM_LIT:1> , '<STR_LIT>' <EOL> assert len ( AuditLog . get_entries ( session , on_user_id = gary_id , <EOL> category = AuditLogCategory . audit ) ) == <NUM_LIT:1> , '<STR_LIT>' </s>
