<s> from django . contrib . sites . shortcuts import get_current_site <EOL> from django . core import mail <EOL> from django . core . urlresolvers import reverse <EOL> from django . test import TestCase <EOL> from django . utils . translation import ugettext as _ <EOL> from connect . config . factories import SiteConfigFactory <EOL> from connect . accounts . factories import ( <EOL> InvitedPendingFactory , RoleFactory , SkillFactory , UserFactory <EOL> ) <EOL> from connect . accounts . forms import ( <EOL> ActivateAccountForm , CloseAccountForm , <EOL> CustomUserCreationForm , CustomUserChangeForm , <EOL> CustomPasswordResetForm , ProfileForm , UpdateEmailForm , <EOL> UpdatePasswordForm <EOL> ) <EOL> from connect . accounts . models import UserSkill <EOL> class CustomCustomPasswordResetFormTest ( TestCase ) : <EOL> """<STR_LIT>""" <EOL> def setUp ( self ) : <EOL> self . user = UserFactory ( email = '<STR_LIT>' ) <EOL> def test_invalid_email ( self ) : <EOL> form = CustomPasswordResetForm ( { '<STR_LIT:email>' : '<STR_LIT>' } ) <EOL> self . assertFalse ( form . is_valid ( ) ) <EOL> self . assertEqual ( form [ '<STR_LIT:email>' ] . errors , <EOL> [ _ ( '<STR_LIT>' ) ] ) <EOL> def test_nonexistent_email ( self ) : <EOL> """<STR_LIT>""" <EOL> form = CustomPasswordResetForm ( { '<STR_LIT:email>' : '<STR_LIT>' } ) <EOL> self . assertTrue ( form . is_valid ( ) ) <EOL> self . assertEqual ( len ( mail . outbox ) , <NUM_LIT:0> ) <EOL> def test_cleaned_data ( self ) : <EOL> form = CustomPasswordResetForm ( { '<STR_LIT:email>' : self . user . email } ) <EOL> self . assertTrue ( form . is_valid ( ) ) <EOL> form . save ( domain_override = '<STR_LIT>' ) <EOL> self . assertEqual ( form . cleaned_data [ '<STR_LIT:email>' ] , self . user . email ) <EOL> self . assertEqual ( len ( mail . outbox ) , <NUM_LIT:1> ) <EOL> def test_custom_email_subject ( self ) : <EOL> data = { '<STR_LIT:email>' : '<STR_LIT>' } <EOL> form = CustomPasswordResetForm ( data ) <EOL> self . assertTrue ( form . is_valid ( ) ) <EOL> form . save ( domain_override = '<STR_LIT>' ) <EOL> self . assertEqual ( len ( mail . outbox ) , <NUM_LIT:1> ) <EOL> self . assertEqual ( mail . outbox [ <NUM_LIT:0> ] . subject , <EOL> '<STR_LIT>' ) <EOL> def test_inactive_user ( self ) : <EOL> """<STR_LIT>""" <EOL> self . user . is_active = False <EOL> self . user . save ( ) <EOL> form = CustomPasswordResetForm ( { '<STR_LIT:email>' : self . user . email } ) <EOL> self . assertTrue ( form . is_valid ( ) ) <EOL> form . save ( ) <EOL> self . assertEqual ( len ( mail . outbox ) , <NUM_LIT:0> ) <EOL> def test_unusable_password ( self ) : <EOL> data = { "<STR_LIT:email>" : "<STR_LIT>" } <EOL> form = CustomPasswordResetForm ( data ) <EOL> self . assertTrue ( form . is_valid ( ) ) <EOL> self . user . set_unusable_password ( ) <EOL> self . user . save ( ) <EOL> form = CustomPasswordResetForm ( data ) <EOL> self . assertTrue ( form . is_valid ( ) ) <EOL> form . save ( ) <EOL> self . assertEqual ( len ( mail . outbox ) , <NUM_LIT:0> ) <EOL> class CustomUserAdminTest ( TestCase ) : <EOL> def test_user_creation_form ( self ) : <EOL> """<STR_LIT>""" <EOL> form = CustomUserCreationForm ( ) <EOL> self . assertNotIn ( '<STR_LIT:username>' , form . fields ) <EOL> def test_user_change_form ( self ) : <EOL> """<STR_LIT>""" <EOL> form = CustomUserChangeForm ( ) <EOL> self . assertNotIn ( '<STR_LIT:username>' , form . fields ) <EOL> class RequestInvitationFormTest ( TestCase ) : <EOL> def setUp ( self ) : <EOL> site = get_current_site ( self . client . request ) <EOL> site . config = SiteConfigFactory ( site = site ) <EOL> self . closed_user = UserFactory ( <EOL> email = '<STR_LIT>' , <EOL> is_closed = True , <EOL> ) <EOL> self . existing_user = UserFactory ( <EOL> email = '<STR_LIT>' , <EOL> ) <EOL> def post_data ( self , email ) : <EOL> return self . client . post ( <EOL> reverse ( '<STR_LIT>' ) , <EOL> data = { <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT:email>' : email , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> } <EOL> ) <EOL> def test_closed_account ( self ) : <EOL> """<STR_LIT>""" <EOL> response = self . post_data ( email = '<STR_LIT>' ) <EOL> self . assertFormError ( <EOL> response , <EOL> form = '<STR_LIT>' , <EOL> field = '<STR_LIT:email>' , <EOL> errors = '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> ) <EOL> def test_registered_email ( self ) : <EOL> """<STR_LIT>""" <EOL> response = self . post_data ( email = '<STR_LIT>' ) <EOL> self . assertFormError ( <EOL> response , <EOL> form = '<STR_LIT>' , <EOL> field = '<STR_LIT:email>' , <EOL> errors = ( '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> ) <EOL> class ActivateAccountFormTest ( TestCase ) : <EOL> def setUp ( self ) : <EOL> site = get_current_site ( self . client . request ) <EOL> site . config = SiteConfigFactory ( site = site ) <EOL> self . pending_user = InvitedPendingFactory ( ) <EOL> def test_unmatched_passwords ( self ) : <EOL> response = self . client . post ( <EOL> reverse ( '<STR_LIT>' , <EOL> args = ( self . pending_user . auth_token , ) ) , <EOL> data = { <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT:password>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> } <EOL> ) <EOL> self . assertFormError ( <EOL> response , <EOL> '<STR_LIT>' , <EOL> field = None , <EOL> errors = '<STR_LIT>' <EOL> ) <EOL> def test_matching_passwords ( self ) : <EOL> form = ActivateAccountForm ( <EOL> user = self . pending_user , <EOL> data = { <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT:password>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> } <EOL> ) <EOL> self . assertTrue ( form . is_valid ( ) ) <EOL> class ProfileFormTest ( TestCase ) : <EOL> def setUp ( self ) : <EOL> self . mentor = RoleFactory ( name = '<STR_LIT>' ) <EOL> self . mentee = RoleFactory ( name = '<STR_LIT>' ) <EOL> self . standard_user = UserFactory ( ) <EOL> self . client . login ( username = self . standard_user . email , password = '<STR_LIT>' ) <EOL> def form_data ( self , name ) : <EOL> return ProfileForm ( <EOL> user = self . standard_user , <EOL> data = { <EOL> '<STR_LIT>' : name , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : [ self . mentor . id , self . mentee . id ] , <EOL> } <EOL> ) <EOL> def test_valid_data ( self ) : <EOL> form = self . form_data ( '<STR_LIT>' ) <EOL> self . assertTrue ( form . is_valid ( ) ) <EOL> def test_missing_full_name ( self ) : <EOL> form = self . form_data ( '<STR_LIT>' ) <EOL> errors = form [ '<STR_LIT>' ] . errors . as_data ( ) <EOL> self . assertEqual ( len ( errors ) , <NUM_LIT:1> ) <EOL> self . assertEqual ( errors [ <NUM_LIT:0> ] . code , '<STR_LIT>' ) <EOL> class SkillFormsetTest ( TestCase ) : <EOL> def setUp ( self ) : <EOL> site = get_current_site ( self . client . request ) <EOL> site . config = SiteConfigFactory ( site = site ) <EOL> self . standard_user = UserFactory ( ) <EOL> self . django = SkillFactory ( name = '<STR_LIT>' ) <EOL> self . rails = SkillFactory ( name = '<STR_LIT>' ) <EOL> self . jquery = SkillFactory ( name = '<STR_LIT>' ) <EOL> self . client . login ( username = self . standard_user . email , password = '<STR_LIT>' ) <EOL> def form_data ( self , skill , proficiency ) : <EOL> return ProfileForm ( <EOL> user = self . standard_user , <EOL> data = { <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : <NUM_LIT:0> , <EOL> '<STR_LIT>' : <NUM_LIT:0> , <EOL> '<STR_LIT>' : <NUM_LIT:1> , <EOL> '<STR_LIT>' : <NUM_LIT:0> , <EOL> '<STR_LIT>' : skill , <EOL> '<STR_LIT>' : proficiency , <EOL> } <EOL> ) <EOL> def post_data ( self , skill1 , proficiency1 , skill2 = '<STR_LIT>' , proficiency2 = '<STR_LIT>' ) : <EOL> return self . client . post ( <EOL> reverse ( '<STR_LIT>' ) , <EOL> data = { <EOL> '<STR_LIT>' : <NUM_LIT:0> , <EOL> '<STR_LIT>' : <NUM_LIT:0> , <EOL> '<STR_LIT>' : <NUM_LIT:2> , <EOL> '<STR_LIT>' : <NUM_LIT:0> , <EOL> '<STR_LIT>' : skill1 , <EOL> '<STR_LIT>' : proficiency1 , <EOL> '<STR_LIT>' : skill2 , <EOL> '<STR_LIT>' : proficiency2 , <EOL> } <EOL> ) <EOL> def raise_formset_error ( self , response , error ) : <EOL> return self . assertFormsetError ( <EOL> response , <EOL> formset = '<STR_LIT>' , <EOL> form_index = None , <EOL> field = None , <EOL> errors = error <EOL> ) <EOL> def test_valid_data ( self ) : <EOL> form = self . form_data ( self . django . id , UserSkill . BEGINNER ) <EOL> self . assertTrue ( form . is_valid ( ) ) <EOL> def test_empty_fields ( self ) : <EOL> """<STR_LIT>""" <EOL> form = self . form_data ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> self . assertTrue ( form . is_valid ( ) ) <EOL> def test_duplicate_skills ( self ) : <EOL> """<STR_LIT>""" <EOL> self . client . login ( username = self . standard_user . email , password = '<STR_LIT>' ) <EOL> response = self . post_data ( self . django . id , UserSkill . BEGINNER , <EOL> self . django . id , UserSkill . INTERMEDIATE ) <EOL> self . raise_formset_error ( response , <EOL> '<STR_LIT>' ) <EOL> def test_skill_without_proficiency ( self ) : <EOL> """<STR_LIT>""" <EOL> self . client . login ( username = self . standard_user . email , password = '<STR_LIT>' ) <EOL> response = self . post_data ( self . django . id , '<STR_LIT>' ) <EOL> self . raise_formset_error ( response , <EOL> '<STR_LIT>' ) <EOL> def test_proficiency_without_skill_name ( self ) : <EOL> """<STR_LIT>""" <EOL> self . client . login ( username = self . standard_user . email , password = '<STR_LIT>' ) <EOL> response = self . post_data ( '<STR_LIT>' , UserSkill . BEGINNER ) <EOL> self . raise_formset_error ( response , <EOL> '<STR_LIT>' ) <EOL> class LinkFormsetTest ( TestCase ) : <EOL> def setUp ( self ) : <EOL> site = get_current_site ( self . client . request ) <EOL> site . config = SiteConfigFactory ( site = site ) <EOL> self . standard_user = UserFactory ( ) <EOL> self . client . login ( username = self . standard_user . email , password = '<STR_LIT>' ) <EOL> def form_data ( self , anchor , url ) : <EOL> return ProfileForm ( <EOL> user = self . standard_user , <EOL> data = { <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : <NUM_LIT:1> , <EOL> '<STR_LIT>' : <NUM_LIT:0> , <EOL> '<STR_LIT>' : <NUM_LIT:0> , <EOL> '<STR_LIT>' : <NUM_LIT:0> , <EOL> '<STR_LIT>' : anchor , <EOL> '<STR_LIT>' : url , <EOL> } <EOL> ) <EOL> def post_data ( self , anchor1 , url1 , anchor2 = '<STR_LIT>' , url2 = '<STR_LIT>' ) : <EOL> return self . client . post ( <EOL> reverse ( '<STR_LIT>' ) , <EOL> data = { <EOL> '<STR_LIT>' : <NUM_LIT:2> , <EOL> '<STR_LIT>' : <NUM_LIT:0> , <EOL> '<STR_LIT>' : <NUM_LIT:0> , <EOL> '<STR_LIT>' : <NUM_LIT:0> , <EOL> '<STR_LIT>' : anchor1 , <EOL> '<STR_LIT>' : url1 , <EOL> '<STR_LIT>' : anchor2 , <EOL> '<STR_LIT>' : url2 , <EOL> } <EOL> ) <EOL> def raise_formset_error ( self , response , error ) : <EOL> self . assertFormsetError ( <EOL> response , <EOL> formset = '<STR_LIT>' , <EOL> form_index = None , <EOL> field = None , <EOL> errors = error <EOL> ) <EOL> def test_valid_data ( self ) : <EOL> form = self . form_data ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> self . assertTrue ( form . is_valid ( ) ) <EOL> def test_empty_fields ( self ) : <EOL> """<STR_LIT>""" <EOL> form = self . form_data ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> self . assertTrue ( form . is_valid ( ) ) <EOL> def test_duplicate_anchors ( self ) : <EOL> """<STR_LIT>""" <EOL> self . client . login ( username = self . standard_user . email , password = '<STR_LIT>' ) <EOL> response = self . post_data ( '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' ) <EOL> self . raise_formset_error ( response , <EOL> '<STR_LIT>' ) <EOL> def test_duplicate_url ( self ) : <EOL> """<STR_LIT>""" <EOL> self . client . login ( username = self . standard_user . email , password = '<STR_LIT>' ) <EOL> response = self . post_data ( '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' ) <EOL> self . raise_formset_error ( response , <EOL> '<STR_LIT>' ) <EOL> def test_anchor_without_url ( self ) : <EOL> """<STR_LIT>""" <EOL> self . client . login ( username = self . standard_user . email , password = '<STR_LIT>' ) <EOL> response = self . post_data ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> self . raise_formset_error ( response , '<STR_LIT>' ) <EOL> def test_url_without_anchor ( self ) : <EOL> """<STR_LIT>""" <EOL> self . client . login ( username = self . standard_user . email , password = '<STR_LIT>' ) <EOL> response = self . post_data ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> self . raise_formset_error ( response , '<STR_LIT>' ) <EOL> class UpdateEmailFormTest ( TestCase ) : <EOL> def setUp ( self ) : <EOL> self . standard_user = UserFactory ( email = '<STR_LIT>' ) <EOL> self . existing_user = UserFactory ( email = '<STR_LIT>' ) <EOL> def form_data ( self , email = '<STR_LIT>' , password = '<STR_LIT>' ) : <EOL> return UpdateEmailForm ( <EOL> user = self . standard_user , <EOL> data = { <EOL> '<STR_LIT:email>' : email , <EOL> '<STR_LIT:password>' : password , <EOL> } <EOL> ) <EOL> def test_valid_data ( self ) : <EOL> form = self . form_data ( ) <EOL> self . assertTrue ( form . is_valid ( ) ) <EOL> def test_incorrect_password ( self ) : <EOL> form = self . form_data ( password = '<STR_LIT>' ) <EOL> errors = form [ '<STR_LIT:password>' ] . errors . as_data ( ) <EOL> self . assertEqual ( len ( errors ) , <NUM_LIT:1> ) <EOL> self . assertEqual ( errors [ <NUM_LIT:0> ] . code , '<STR_LIT>' ) <EOL> def test_registered_email ( self ) : <EOL> """<STR_LIT>""" <EOL> form = self . form_data ( '<STR_LIT>' ) <EOL> errors = form [ '<STR_LIT:email>' ] . errors . as_data ( ) <EOL> self . assertEqual ( len ( errors ) , <NUM_LIT:1> ) <EOL> self . assertEqual ( errors [ <NUM_LIT:0> ] . code , '<STR_LIT>' ) <EOL> class UpdatePasswordFormTest ( TestCase ) : <EOL> def setUp ( self ) : <EOL> self . standard_user = UserFactory ( email = '<STR_LIT>' ) <EOL> def form_data ( self , new = '<STR_LIT>' , current = '<STR_LIT>' ) : <EOL> return UpdatePasswordForm ( <EOL> user = self . standard_user , <EOL> data = { <EOL> '<STR_LIT>' : new , <EOL> '<STR_LIT>' : current , <EOL> } <EOL> ) <EOL> def test_valid_data ( self ) : <EOL> form = self . form_data ( ) <EOL> self . assertTrue ( form . is_valid ( ) ) <EOL> def test_missing_new_password ( self ) : <EOL> form = self . form_data ( '<STR_LIT>' ) <EOL> errors = form [ '<STR_LIT>' ] . errors . as_data ( ) <EOL> self . assertEqual ( len ( errors ) , <NUM_LIT:1> ) <EOL> self . assertEqual ( errors [ <NUM_LIT:0> ] . code , '<STR_LIT>' ) <EOL> def test_incorrect_current_password ( self ) : <EOL> form = self . form_data ( current = '<STR_LIT>' ) <EOL> errors = form [ '<STR_LIT>' ] . errors . as_data ( ) <EOL> self . assertEqual ( len ( errors ) , <NUM_LIT:1> ) <EOL> self . assertEqual ( errors [ <NUM_LIT:0> ] . code , '<STR_LIT>' ) <EOL> class CloseAccountFormTest ( TestCase ) : <EOL> def setUp ( self ) : <EOL> self . standard_user = UserFactory ( ) <EOL> def form_data ( self , password = '<STR_LIT>' ) : <EOL> return CloseAccountForm ( <EOL> user = self . standard_user , <EOL> data = { '<STR_LIT:password>' : password } <EOL> ) <EOL> def test_valid_data ( self ) : <EOL> form = self . form_data ( ) <EOL> self . assertTrue ( form . is_valid ( ) ) <EOL> def test_no_password ( self ) : <EOL> form = self . form_data ( '<STR_LIT>' ) <EOL> errors = form [ '<STR_LIT:password>' ] . errors . as_data ( ) <EOL> self . assertEqual ( len ( errors ) , <NUM_LIT:1> ) <EOL> self . assertEqual ( errors [ <NUM_LIT:0> ] . code , '<STR_LIT>' ) <EOL> def test_incorrect_password ( self ) : <EOL> form = self . form_data ( '<STR_LIT>' ) <EOL> errors = form [ '<STR_LIT:password>' ] . errors . as_data ( ) <EOL> self . assertEqual ( len ( errors ) , <NUM_LIT:1> ) <EOL> self . assertEqual ( errors [ <NUM_LIT:0> ] . code , '<STR_LIT>' ) </s>
