<s> from django . core . urlresolvers import reverse <EOL> from rest_framework import status <EOL> from rest_framework . test import APIClient <EOL> from openslides . core . config import config <EOL> from openslides . users . models import Group , User <EOL> from openslides . utils . test import TestCase <EOL> class UserGetTest ( TestCase ) : <EOL> """<STR_LIT>""" <EOL> def test_get_with_user_who_is_in_group_with_pk_1 ( self ) : <EOL> """<STR_LIT>""" <EOL> admin = User . objects . get ( pk = <NUM_LIT:1> ) <EOL> group1 = Group . objects . get ( pk = <NUM_LIT:1> ) <EOL> admin . groups . add ( group1 ) <EOL> self . client . login ( username = '<STR_LIT>' , password = '<STR_LIT>' ) <EOL> response = self . client . get ( '<STR_LIT>' ) <EOL> self . assertEqual ( response . status_code , <NUM_LIT:200> ) <EOL> class UserCreate ( TestCase ) : <EOL> """<STR_LIT>""" <EOL> def test_simple_creation ( self ) : <EOL> self . client . login ( username = '<STR_LIT>' , password = '<STR_LIT>' ) <EOL> response = self . client . post ( <EOL> reverse ( '<STR_LIT>' ) , <EOL> { '<STR_LIT>' : '<STR_LIT>' } ) <EOL> self . assertEqual ( response . status_code , status . HTTP_201_CREATED ) <EOL> new_user = User . objects . get ( username = '<STR_LIT>' ) <EOL> self . assertEqual ( response . data [ '<STR_LIT:id>' ] , new_user . id ) <EOL> def test_creation_with_group ( self ) : <EOL> self . client . login ( username = '<STR_LIT>' , password = '<STR_LIT>' ) <EOL> group_pks = ( <NUM_LIT:3> , <NUM_LIT:4> , ) <EOL> self . client . post ( <EOL> reverse ( '<STR_LIT>' ) , <EOL> { '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : group_pks } ) <EOL> user = User . objects . get ( username = '<STR_LIT>' ) <EOL> self . assertTrue ( user . groups . filter ( pk = group_pks [ <NUM_LIT:0> ] ) . exists ( ) ) <EOL> self . assertTrue ( user . groups . filter ( pk = group_pks [ <NUM_LIT:1> ] ) . exists ( ) ) <EOL> def test_creation_with_anonymous_or_registered_group ( self ) : <EOL> self . client . login ( username = '<STR_LIT>' , password = '<STR_LIT>' ) <EOL> group_pks = ( <NUM_LIT:1> , <NUM_LIT:2> , ) <EOL> response = self . client . post ( <EOL> reverse ( '<STR_LIT>' ) , <EOL> { '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : group_pks } ) <EOL> self . assertEqual ( response . status_code , status . HTTP_400_BAD_REQUEST ) <EOL> self . assertEqual ( response . data , { '<STR_LIT>' : [ "<STR_LIT>" % group_pks [ <NUM_LIT:0> ] ] } ) <EOL> class UserUpdate ( TestCase ) : <EOL> """<STR_LIT>""" <EOL> def test_simple_update_via_patch ( self ) : <EOL> """<STR_LIT>""" <EOL> admin_client = APIClient ( ) <EOL> admin_client . login ( username = '<STR_LIT>' , password = '<STR_LIT>' ) <EOL> user_pk = <NUM_LIT:1> <EOL> response = admin_client . patch ( <EOL> reverse ( '<STR_LIT>' , args = [ user_pk ] ) , <EOL> { '<STR_LIT>' : '<STR_LIT>' } ) <EOL> self . assertEqual ( response . status_code , status . HTTP_200_OK ) <EOL> user = User . objects . get ( pk = user_pk ) <EOL> self . assertEqual ( user . last_name , '<STR_LIT>' ) <EOL> self . assertEqual ( user . username , '<STR_LIT>' ) <EOL> def test_simple_update_via_put ( self ) : <EOL> """<STR_LIT>""" <EOL> admin_client = APIClient ( ) <EOL> admin_client . login ( username = '<STR_LIT>' , password = '<STR_LIT>' ) <EOL> user_pk = <NUM_LIT:1> <EOL> response = admin_client . put ( <EOL> reverse ( '<STR_LIT>' , args = [ user_pk ] ) , <EOL> { '<STR_LIT>' : '<STR_LIT>' } ) <EOL> self . assertEqual ( response . status_code , <NUM_LIT:200> ) <EOL> self . assertEqual ( User . objects . get ( pk = <NUM_LIT:1> ) . username , '<STR_LIT>' ) <EOL> def test_update_deactivate_yourselfself ( self ) : <EOL> """<STR_LIT>""" <EOL> admin_client = APIClient ( ) <EOL> admin_client . login ( username = '<STR_LIT>' , password = '<STR_LIT>' ) <EOL> user_pk = <NUM_LIT:1> <EOL> response = admin_client . patch ( <EOL> reverse ( '<STR_LIT>' , args = [ user_pk ] ) , <EOL> { '<STR_LIT:username>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : False } , <EOL> format = '<STR_LIT>' ) <EOL> self . assertEqual ( response . status_code , status . HTTP_400_BAD_REQUEST ) <EOL> class UserDelete ( TestCase ) : <EOL> """<STR_LIT>""" <EOL> def test_delete ( self ) : <EOL> admin_client = APIClient ( ) <EOL> admin_client . login ( username = '<STR_LIT>' , password = '<STR_LIT>' ) <EOL> User . objects . create ( username = '<STR_LIT>' ) <EOL> response = admin_client . delete ( reverse ( '<STR_LIT>' , args = [ '<STR_LIT:2>' ] ) ) <EOL> self . assertEqual ( response . status_code , status . HTTP_204_NO_CONTENT ) <EOL> self . assertFalse ( User . objects . filter ( username = '<STR_LIT>' ) . exists ( ) ) <EOL> def test_delete_yourself ( self ) : <EOL> admin_client = APIClient ( ) <EOL> admin_client . login ( username = '<STR_LIT>' , password = '<STR_LIT>' ) <EOL> admin_user_pk = <NUM_LIT:1> <EOL> response = admin_client . delete ( reverse ( '<STR_LIT>' , args = [ admin_user_pk ] ) ) <EOL> self . assertEqual ( response . status_code , status . HTTP_400_BAD_REQUEST ) <EOL> class UserResetPassword ( TestCase ) : <EOL> """<STR_LIT>""" <EOL> def test_reset ( self ) : <EOL> admin_client = APIClient ( ) <EOL> admin_client . login ( username = '<STR_LIT>' , password = '<STR_LIT>' ) <EOL> user = User . objects . create ( username = '<STR_LIT>' ) <EOL> user . default_password = '<STR_LIT>' <EOL> user . save ( ) <EOL> response = admin_client . post ( <EOL> reverse ( '<STR_LIT>' , args = [ user . pk ] ) , <EOL> { '<STR_LIT:password>' : '<STR_LIT>' } ) <EOL> self . assertEqual ( response . status_code , status . HTTP_200_OK ) <EOL> self . assertTrue ( User . objects . get ( pk = user . pk ) . check_password ( <EOL> '<STR_LIT>' ) ) <EOL> def test_reset_to_default ( self ) : <EOL> admin_client = APIClient ( ) <EOL> admin_client . login ( username = '<STR_LIT>' , password = '<STR_LIT>' ) <EOL> user = User . objects . create ( username = '<STR_LIT>' ) <EOL> user . default_password = '<STR_LIT>' <EOL> user . save ( ) <EOL> response = admin_client . post ( <EOL> reverse ( '<STR_LIT>' , args = [ user . pk ] ) , { } ) <EOL> self . assertEqual ( response . status_code , status . HTTP_200_OK ) <EOL> self . assertTrue ( User . objects . get ( pk = user . pk ) . check_password ( <EOL> '<STR_LIT>' ) ) <EOL> class GroupReceive ( TestCase ) : <EOL> def test_get_groups_as_anonymous_deactivated ( self ) : <EOL> """<STR_LIT>""" <EOL> response = self . client . get ( '<STR_LIT>' ) <EOL> self . assertEqual ( response . status_code , <NUM_LIT> ) <EOL> def test_get_groups_as_anonymous_user_activated ( self ) : <EOL> """<STR_LIT>""" <EOL> config [ '<STR_LIT>' ] = True <EOL> response = self . client . get ( '<STR_LIT>' ) <EOL> self . assertEqual ( response . status_code , <NUM_LIT:200> ) <EOL> def test_logged_in_user_with_no_permission ( self ) : <EOL> """<STR_LIT>""" <EOL> user = User ( username = '<STR_LIT:test>' ) <EOL> user . set_password ( '<STR_LIT:test>' ) <EOL> user . save ( ) <EOL> registered_group = Group . objects . get ( pk = <NUM_LIT:2> ) <EOL> registered_group . permissions . all ( ) . delete ( ) <EOL> self . client . login ( username = '<STR_LIT:test>' , password = '<STR_LIT:test>' ) <EOL> response = self . client . get ( '<STR_LIT>' ) <EOL> self . assertEqual ( response . status_code , <NUM_LIT:200> ) <EOL> class GroupCreate ( TestCase ) : <EOL> """<STR_LIT>""" <EOL> def test_creation ( self ) : <EOL> self . client . login ( username = '<STR_LIT>' , password = '<STR_LIT>' ) <EOL> permissions = ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> response = self . client . post ( <EOL> reverse ( '<STR_LIT>' ) , <EOL> { '<STR_LIT:name>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : permissions } ) <EOL> self . assertEqual ( response . status_code , status . HTTP_201_CREATED ) <EOL> group = Group . objects . get ( name = '<STR_LIT>' ) <EOL> for permission in permissions : <EOL> app_label , codename = permission . split ( '<STR_LIT:.>' ) <EOL> self . assertTrue ( group . permissions . get ( content_type__app_label = app_label , codename = codename ) ) <EOL> def test_failed_creation_invalid_value ( self ) : <EOL> self . client . login ( username = '<STR_LIT>' , password = '<STR_LIT>' ) <EOL> permissions = ( '<STR_LIT>' , ) <EOL> response = self . client . post ( <EOL> reverse ( '<STR_LIT>' ) , <EOL> { '<STR_LIT:name>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : permissions } ) <EOL> self . assertEqual ( response . status_code , status . HTTP_400_BAD_REQUEST ) <EOL> self . assertEqual ( <EOL> response . data , <EOL> { '<STR_LIT>' : [ '<STR_LIT>' ] } ) <EOL> def test_failed_creation_invalid_permission ( self ) : <EOL> self . client . login ( username = '<STR_LIT>' , password = '<STR_LIT>' ) <EOL> permissions = ( '<STR_LIT>' , ) <EOL> response = self . client . post ( <EOL> reverse ( '<STR_LIT>' ) , <EOL> { '<STR_LIT:name>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : permissions } ) <EOL> self . assertEqual ( response . status_code , status . HTTP_400_BAD_REQUEST ) <EOL> self . assertEqual ( <EOL> response . data , <EOL> { '<STR_LIT>' : [ '<STR_LIT>' ] } ) <EOL> class GroupUpdate ( TestCase ) : <EOL> """<STR_LIT>""" <EOL> def test_simple_update_via_patch ( self ) : <EOL> admin_client = APIClient ( ) <EOL> admin_client . login ( username = '<STR_LIT>' , password = '<STR_LIT>' ) <EOL> group_pk = <NUM_LIT:3> <EOL> permissions = ( '<STR_LIT>' , ) <EOL> response = admin_client . patch ( <EOL> reverse ( '<STR_LIT>' , args = [ group_pk ] ) , <EOL> { '<STR_LIT>' : permissions } ) <EOL> self . assertEqual ( response . status_code , status . HTTP_200_OK ) <EOL> group = Group . objects . get ( pk = group_pk ) <EOL> for permission in permissions : <EOL> app_label , codename = permission . split ( '<STR_LIT:.>' ) <EOL> self . assertTrue ( group . permissions . get ( content_type__app_label = app_label , codename = codename ) ) <EOL> def test_simple_update_via_put ( self ) : <EOL> admin_client = APIClient ( ) <EOL> admin_client . login ( username = '<STR_LIT>' , password = '<STR_LIT>' ) <EOL> group_pk = <NUM_LIT:3> <EOL> permissions = ( '<STR_LIT>' , ) <EOL> response = admin_client . put ( <EOL> reverse ( '<STR_LIT>' , args = [ group_pk ] ) , <EOL> { '<STR_LIT>' : permissions } ) <EOL> self . assertEqual ( response . status_code , status . HTTP_400_BAD_REQUEST ) <EOL> self . assertEqual ( response . data , { '<STR_LIT:name>' : [ '<STR_LIT>' ] } ) <EOL> class GroupDelete ( TestCase ) : <EOL> """<STR_LIT>""" <EOL> def test_delete ( self ) : <EOL> admin_client = APIClient ( ) <EOL> admin_client . login ( username = '<STR_LIT>' , password = '<STR_LIT>' ) <EOL> group = Group . objects . create ( name = '<STR_LIT>' ) <EOL> response = admin_client . delete ( reverse ( '<STR_LIT>' , args = [ group . pk ] ) ) <EOL> self . assertEqual ( response . status_code , status . HTTP_204_NO_CONTENT ) <EOL> self . assertFalse ( Group . objects . filter ( name = '<STR_LIT>' ) . exists ( ) ) <EOL> def test_delete_builtin_groups ( self ) : <EOL> admin_client = APIClient ( ) <EOL> admin_client . login ( username = '<STR_LIT>' , password = '<STR_LIT>' ) <EOL> group_pks = ( <NUM_LIT:1> , <NUM_LIT:2> , ) <EOL> for group_pk in group_pks : <EOL> response = admin_client . delete ( reverse ( '<STR_LIT>' , args = [ group_pk ] ) ) <EOL> self . assertEqual ( response . status_code , status . HTTP_403_FORBIDDEN ) </s>
