|  | @@ -0,0 +1,88 @@
 | 
	
		
			
			|  | 1 | +# -*- coding: utf-8 -*-
 | 
	
		
			
			|  | 2 | +from collections import OrderedDict
 | 
	
		
			
			|  | 3 | +
 | 
	
		
			
			|  | 4 | +from nose.tools import eq_
 | 
	
		
			
			|  | 5 | +from nose.tools import ok_
 | 
	
		
			
			|  | 6 | +
 | 
	
		
			
			|  | 7 | +from tracim.model import DBSession
 | 
	
		
			
			|  | 8 | +from tracim.model import User
 | 
	
		
			
			|  | 9 | +from tracim.tests import TracimTestController
 | 
	
		
			
			|  | 10 | +
 | 
	
		
			
			|  | 11 | +
 | 
	
		
			
			|  | 12 | +class TestAuthentication(TracimTestController):
 | 
	
		
			
			|  | 13 | +    application_under_test = 'main'
 | 
	
		
			
			|  | 14 | +
 | 
	
		
			
			|  | 15 | +    def test_create_user(self):
 | 
	
		
			
			|  | 16 | +        self._connect_user(
 | 
	
		
			
			|  | 17 | +            'admin@admin.admin',
 | 
	
		
			
			|  | 18 | +            'admin@admin.admin',
 | 
	
		
			
			|  | 19 | +        )
 | 
	
		
			
			|  | 20 | +
 | 
	
		
			
			|  | 21 | +        user_count = DBSession.query(User) \
 | 
	
		
			
			|  | 22 | +            .filter(User.email == 'an-other-email@test.local').count()
 | 
	
		
			
			|  | 23 | +        eq_(0, user_count, 'User should not exist yet')
 | 
	
		
			
			|  | 24 | +
 | 
	
		
			
			|  | 25 | +        # Create a new user
 | 
	
		
			
			|  | 26 | +        try_post_user = self.app.post(
 | 
	
		
			
			|  | 27 | +            '/admin/users',
 | 
	
		
			
			|  | 28 | +            OrderedDict([
 | 
	
		
			
			|  | 29 | +                ('name', 'TEST'),
 | 
	
		
			
			|  | 30 | +                ('email', 'an-other-email@test.local'),
 | 
	
		
			
			|  | 31 | +                ('password', 'password'),
 | 
	
		
			
			|  | 32 | +                ('is_tracim_manager', 'off'),
 | 
	
		
			
			|  | 33 | +                ('is_tracim_admin', 'off'),
 | 
	
		
			
			|  | 34 | +                ('send_email', 'off'),
 | 
	
		
			
			|  | 35 | +            ])
 | 
	
		
			
			|  | 36 | +        )
 | 
	
		
			
			|  | 37 | +
 | 
	
		
			
			|  | 38 | +        eq_(try_post_user.status_code, 302,
 | 
	
		
			
			|  | 39 | +            "Code should be 302, but is %d" % try_post_user.status_code)
 | 
	
		
			
			|  | 40 | +
 | 
	
		
			
			|  | 41 | +        user = DBSession.query(User) \
 | 
	
		
			
			|  | 42 | +            .filter(User.email == 'an-other-email@test.local').one()
 | 
	
		
			
			|  | 43 | +        ok_(user, msg="User should exist now")
 | 
	
		
			
			|  | 44 | +        ok_(user.validate_password('password'))
 | 
	
		
			
			|  | 45 | +
 | 
	
		
			
			|  | 46 | +        # User must have webdav digest
 | 
	
		
			
			|  | 47 | +        ok_(user.webdav_left_digest_response_hash)
 | 
	
		
			
			|  | 48 | +
 | 
	
		
			
			|  | 49 | +    def test_update_user_password(self):
 | 
	
		
			
			|  | 50 | +        self._connect_user(
 | 
	
		
			
			|  | 51 | +            'admin@admin.admin',
 | 
	
		
			
			|  | 52 | +            'admin@admin.admin',
 | 
	
		
			
			|  | 53 | +        )
 | 
	
		
			
			|  | 54 | +
 | 
	
		
			
			|  | 55 | +        # Create a new user (tested in test_create_user)
 | 
	
		
			
			|  | 56 | +        self.app.post(
 | 
	
		
			
			|  | 57 | +            '/admin/users',
 | 
	
		
			
			|  | 58 | +            OrderedDict([
 | 
	
		
			
			|  | 59 | +                ('name', 'TEST'),
 | 
	
		
			
			|  | 60 | +                ('email', 'an-other-email@test.local'),
 | 
	
		
			
			|  | 61 | +                ('password', 'an-other-email@test.local'),
 | 
	
		
			
			|  | 62 | +                ('is_tracim_manager', 'off'),
 | 
	
		
			
			|  | 63 | +                ('is_tracim_admin', 'off'),
 | 
	
		
			
			|  | 64 | +                ('send_email', 'off'),
 | 
	
		
			
			|  | 65 | +            ])
 | 
	
		
			
			|  | 66 | +        )
 | 
	
		
			
			|  | 67 | +
 | 
	
		
			
			|  | 68 | +        user = DBSession.query(User) \
 | 
	
		
			
			|  | 69 | +            .filter(User.email == 'an-other-email@test.local').one()
 | 
	
		
			
			|  | 70 | +        webdav_digest = user.webdav_left_digest_response_hash
 | 
	
		
			
			|  | 71 | +
 | 
	
		
			
			|  | 72 | +        self.app.post(
 | 
	
		
			
			|  | 73 | +            '/admin/users/{user_id}/password?_method=PUT'.format(
 | 
	
		
			
			|  | 74 | +                user_id=user.user_id
 | 
	
		
			
			|  | 75 | +            ),
 | 
	
		
			
			|  | 76 | +            OrderedDict([
 | 
	
		
			
			|  | 77 | +                ('new_password1', 'new-password'),
 | 
	
		
			
			|  | 78 | +                ('new_password2', 'new-password'),
 | 
	
		
			
			|  | 79 | +            ])
 | 
	
		
			
			|  | 80 | +        )
 | 
	
		
			
			|  | 81 | +
 | 
	
		
			
			|  | 82 | +        user = DBSession.query(User) \
 | 
	
		
			
			|  | 83 | +            .filter(User.email == 'an-other-email@test.local').one()
 | 
	
		
			
			|  | 84 | +        ok_(user.validate_password('new-password'))
 | 
	
		
			
			|  | 85 | +        ok_(
 | 
	
		
			
			|  | 86 | +            webdav_digest != user.webdav_left_digest_response_hash,
 | 
	
		
			
			|  | 87 | +            msg='Webdav digest should be updated',
 | 
	
		
			
			|  | 88 | +        )
 |