def __init__(self, email, username, password, admin=False): self.email = email self.username = username self.password = bcrypt.generate_password_hash( password, app.config.get('BCRYPT_LOG_ROUNDS')).decode() self.registered_on = datetime.datetime.now() self.admin = admin
def add_user(institution_uri, department_uri): dep = get_department_id(institution_uri, department_uri) inst = get_institution_id(institution_uri) if DepartmentPermission(dep, inst).can(): db = get_db() args = request.get_json() user = User(username=args['username'], password=bcrypt.generate_password_hash(args['password']), department=dep) db.session.add(user) db.session.commit() return '' abort(403)
class TestTools(WebAppTestCase): @mongo_data(users=[{ 'email': '*****@*****.**', 'nickname': 'james', 'password': bcrypt.generate_password_hash('password', rounds=12) }]) def test_index(self): self.client.post('/', data=dict(email='*****@*****.**', password='******')) response = self.client.get('/tools', follow_redirects=True) self.assert200(response) self.assertTemplateUsed('tools/index.html')
def add_institution(): if AdminPermission().can(): db = get_db() args = request.get_json() institution = Institution(name=args['name'], uri=args['uri']) db.session.add(institution) db.session.commit() department = Department(name="admin", uri="admin", institution=institution.id) db.session.add(department) db.session.commit() admin = User(username=args['username'], password=bcrypt.generate_password_hash(args['password']), department=department.id) db.session.add(admin) db.session.commit() return '1' abort(403)
def index(): form = UserForm(nickname=current_user.nickname, email=current_user.email) if request.method == 'POST' and form.validate(): mongo.db.users.update({'_id': ObjectId(current_user.get_id())}, {'$set': {'email': form.email.data, 'nickname': form.nickname.data}}) mongo.db.bookmarks.update({'user._id': ObjectId(current_user.get_id())}, {'$set': {'user.email': form.email.data, 'user.nickname': form.nickname.data}}, multi=True) if form.new_password.data: mongo.db.users.update({'_id': ObjectId(current_user.get_id())}, {'$set': {'password': bcrypt.generate_password_hash(form.new_password.data, rounds=12)}}) flash(u'Les modifications ont été correctement enregistrées.') return render_template('profil/index.html', form=form)
class TestDeleteAllBookmarks(WebAppTestCase): @mongo_data(users=[{ 'email': '*****@*****.**', 'nickname': 'james', 'password': bcrypt.generate_password_hash('password', rounds=12) }]) def test_display_delete_all_bookmarks(self): self.client.post('/', data=dict(email='*****@*****.**', password='******')) response = self.client.get('/tools/delete_all', follow_redirects=True) self.assert200(response) self.assertTemplateUsed('tools/delete_all.html') @mongo_data(users=[{ '_id': ObjectId('5495f2a88766017d44130bb1'), 'email': '*****@*****.**', 'nickname': 'james', 'password': bcrypt.generate_password_hash('password', rounds=12) }, { '_id': ObjectId('5495f2a88766017d44130bb6'), 'email': '*****@*****.**', 'nickname': 'ironman', 'password': bcrypt.generate_password_hash('jarvis', rounds=12) }], bookmarks=[{ 'url': 'http://www.foo.com', 'user': { '_id': ObjectId('5495f2a88766017d44130bb1'), 'email': '*****@*****.**' }, 'published': datetime.datetime.now() }, { 'url': 'http://www.bar.com', 'user': { '_id': ObjectId('5495f2a88766017d44130bb1'), 'email': '*****@*****.**' }, 'published': datetime.datetime.now() }, { 'url': 'http://www.foo.com', 'user': { '_id': ObjectId('5495f2a88766017d44130bb6'), 'email': '*****@*****.**' }, 'published': datetime.datetime.now() }]) def test_delete_all_bookmarks(self): self.client.post('/', data=dict(email='*****@*****.**', password='******')) response = self.client.post('/tools/delete_all', follow_redirects=True) self.assert200(response) self.assertTemplateUsed('bookmarks/public.html') assert_equal( list(mongo.db.bookmarks.find({}, { '_id': 0, 'published': 0 })), [{ 'url': 'http://www.foo.com', 'user': { '_id': ObjectId('5495f2a88766017d44130bb6'), 'email': '*****@*****.**' } }])
class TestImportBookmarks(WebAppTestCase): @mongo_data(users=[{ 'email': '*****@*****.**', 'nickname': 'james', 'password': bcrypt.generate_password_hash('password', rounds=12) }]) def test_display_import_bookmark(self): self.client.post('/', data=dict(email='*****@*****.**', password='******')) response = self.client.get('/tools/import', follow_redirects=True) self.assert200(response) self.assertTemplateUsed('tools/import.html') @mongo_data(users=[{ '_id': ObjectId('5495f2a88766017d44130bb1'), 'email': '*****@*****.**', 'nickname': 'james', 'password': bcrypt.generate_password_hash('password', rounds=12) }], bookmarks=[{ 'url': 'http://www.foo.com', 'user': { '_id': ObjectId('5495f2a88766017d44130bb1'), 'email': '*****@*****.**' }, 'published': datetime.datetime.now() }]) def test_import_bookmarks(self): self.client.post('/', data=dict(email='*****@*****.**', password='******')) response = self.client.post( '/tools/import', data=dict(atom_file=(StringIO(XML_FILE_CONTENT), 'bookmarks.xml'))) self.assert200(response) self.assertTemplateUsed('tools/import.html') self.assert_flashes(u'Fichier importé avec succès !') self.assertEqual(5, mongo.db.bookmarks.find().count()) self.assertEqual( { 'url': 'https://www.simple.com/', 'title': 'Simple | Worry-free Alternative to Traditional Banking', 'referrer': 'http://colin-verdier.com/le-logiciel-devore-le-monde-depuis-les-etats-unis/', 'description': '', 'public': True, 'tags': [], 'user': { '_id': ObjectId('5495f2a88766017d44130bb1'), 'email': '*****@*****.**', 'nickname': 'james' } }, mongo.db.bookmarks.find_one( { 'url': 'https://www.simple.com/', 'user._id': ObjectId('5495f2a88766017d44130bb1') }, { '_id': 0, 'published': 0 })) self.assertEqual( { 'url': 'http://www.foo.com', 'title': 'Update bookmark', 'referrer': 'http://colin-verdier.com/le-logiciel-devore-le-monde-depuis-les-etats-unis/', 'description': 'Test update bookmark', 'public': True, 'tags': [], 'user': { '_id': ObjectId('5495f2a88766017d44130bb1'), 'email': '*****@*****.**', 'nickname': 'james' } }, mongo.db.bookmarks.find_one( { 'url': 'http://www.foo.com', 'user._id': ObjectId('5495f2a88766017d44130bb1') }, { '_id': 0, 'published': 0 })) self.assertEqual( { 'url': 'http://retrospectivewiki.org/index.php?title=Retrospective_Plans', 'title': 'Retrospective Plans - Agile Retrospective Resource Wiki', 'referrer': '', 'description': 'A collection of detailed retrospective plans you can run or take inspiration from', 'public': True, 'tags': ['agile', 'wiki'], 'user': { '_id': ObjectId('5495f2a88766017d44130bb1'), 'email': '*****@*****.**', 'nickname': 'james' } }, mongo.db.bookmarks.find_one( { 'url': 'http://retrospectivewiki.org/index.php?title=Retrospective_Plans', 'user._id': ObjectId('5495f2a88766017d44130bb1') }, { '_id': 0, 'published': 0 })) self.assertEqual( { 'url': 'http://www.asual.com/jquery/address/', 'title': 'Asual | jQuery Address - Deep linking for the masses', 'referrer': '', 'description': 'The jQuery Address plugin provides powerful deep linking', 'public': True, 'tags': [ 'programmation', 'javascript', 'jQuery', 'open source', 'appb' ], 'user': { '_id': ObjectId('5495f2a88766017d44130bb1'), 'email': '*****@*****.**', 'nickname': 'james' } }, mongo.db.bookmarks.find_one( { 'url': 'http://www.asual.com/jquery/address/', 'user._id': ObjectId('5495f2a88766017d44130bb1') }, { '_id': 0, 'published': 0 })) self.assertEqual( { 'url': 'http://europa.eu/epso/index_fr.htm', 'title': 'EUROPA - EPSO', 'referrer': '', 'description': '', 'public': False, 'tags': ['emploi'], 'user': { '_id': ObjectId('5495f2a88766017d44130bb1'), 'email': '*****@*****.**', 'nickname': 'james' } }, mongo.db.bookmarks.find_one( { 'url': 'http://europa.eu/epso/index_fr.htm', 'user._id': ObjectId('5495f2a88766017d44130bb1') }, { '_id': 0, 'published': 0 }))
class TestExportBookmarks(WebAppTestCase): @mongo_data(users=[{ 'email': '*****@*****.**', 'nickname': 'james', 'password': bcrypt.generate_password_hash('password', rounds=12) }]) def test_display_import_bookmark(self): self.client.post('/', data=dict(email='*****@*****.**', password='******')) response = self.client.get('/tools/export', follow_redirects=True) self.assert200(response) self.assertTemplateUsed('tools/export.html') @mongo_data(users=[{ '_id': ObjectId('5495f2a88766017d44130bb1'), 'email': '*****@*****.**', 'nickname': 'james', 'password': bcrypt.generate_password_hash('password', rounds=12) }, { '_id': ObjectId('5495f2a88766017d44130bb6'), 'email': '*****@*****.**', 'nickname': 'ironman', 'password': bcrypt.generate_password_hash('jarvis', rounds=12) }], bookmarks=[{ 'url': 'http://www.foo.com', 'title': 'titre 1', 'referrer': 'http://a', 'description': 'description 1', 'tags': ['test', 'unitaire'], 'user': { '_id': ObjectId('5495f2a88766017d44130bb1'), 'email': '*****@*****.**' }, 'published': datetime.datetime.now() }, { 'url': 'http://www.bar.com', 'title': 'titre 2', 'referrer': 'http://b', 'description': 'description 1', 'tags': ['tag'], 'user': { '_id': ObjectId('5495f2a88766017d44130bb1'), 'email': '*****@*****.**' }, 'published': datetime.datetime.now() }, { 'url': 'http://www.foo.com', 'user': { '_id': ObjectId('5495f2a88766017d44130bb6'), 'email': '*****@*****.**' }, 'published': datetime.datetime.now() }]) def test_export(self): self.client.post('/', data=dict(email='*****@*****.**', password='******')) response = self.client.get('/tools/download_atom', follow_redirects=True) feed = feedparser.parse(response.data) self.assertEqual('Mes bookmarks', feed['feed']['title']) self.assertEquals(2, len(feed.entries))
class TestProfil(WebAppTestCase): @mongo_data(users=[{ 'email': '*****@*****.**', 'nickname': 'james', 'password': bcrypt.generate_password_hash('password', rounds=12) }]) def test_index(self): self.client.post('/', data=dict(email='*****@*****.**', password='******')) response = self.client.get('/profil', follow_redirects=True) self.assert200(response) self.assertTemplateUsed('profil/index.html') @mongo_data(users=[{ 'email': '*****@*****.**', 'nickname': 'james', 'password': bcrypt.generate_password_hash('password', rounds=12) }]) def test_change_password(self): self.client.post('/', data=dict(email='*****@*****.**', password='******')) response = self.client.post('/profil/', data=dict(nickname='james', email='*****@*****.**', actual_password='******', new_password='******', confirm_new_password='******')) self.assert200(response) self.assertTemplateUsed('profil/index.html') self.assertEqual({ 'email': '*****@*****.**', 'nickname': 'james' }, mongo.db.users.find_one({'email': '*****@*****.**'}, { 'password': 0, '_id': 0 })) self.assertTrue( bcrypt.check_password_hash( mongo.db.users.find_one({'email': '*****@*****.**'}, {'password': 1})['password'], 'secret')) @mongo_data(users=[{ '_id': ObjectId('5495f2a88766017d44130bb6'), 'email': '*****@*****.**', 'nickname': 'james', 'password': bcrypt.generate_password_hash('password', rounds=12) }], bookmarks=[{ 'url': 'http://www.foo.com', 'user': { '_id': ObjectId('5495f2a88766017d44130bb6'), 'email': '*****@*****.**', 'nickname': 'james' }, 'published': datetime.datetime.now() }, { 'url': 'http://www.bar.com', 'user': { '_id': ObjectId('5495f2a88766017d44130bb6'), 'email': '*****@*****.**', 'nickname': 'james' }, 'published': datetime.datetime.now() }, { 'url': 'http://www.foo.com', 'user': { '_id': ObjectId('5495f2a88766017d44130bb1'), 'email': '*****@*****.**', 'nickname': 'tony' }, 'published': datetime.datetime.now() }]) def test_change_email(self): self.client.post('/', data=dict(email='*****@*****.**', password='******')) response = self.client.post('/profil/', data=dict(nickname='james', email='*****@*****.**')) self.assert200(response) self.assertTemplateUsed('profil/index.html') self.assertEqual({ 'email': '*****@*****.**', 'nickname': 'james' }, mongo.db.users.find_one({'email': '*****@*****.**'}, { 'password': 0, '_id': 0 })) self.assertTrue( bcrypt.check_password_hash( mongo.db.users.find_one({'email': '*****@*****.**'}, {'password': 1})['password'], 'password')) self.assertEqual( [{ 'url': 'http://www.bar.com', 'user': { '_id': ObjectId('5495f2a88766017d44130bb6'), 'email': '*****@*****.**', 'nickname': 'james' } }, { 'url': 'http://www.foo.com', 'user': { '_id': ObjectId('5495f2a88766017d44130bb6'), 'email': '*****@*****.**', 'nickname': 'james' } }], list( mongo.db.bookmarks.find( { 'user._id': ObjectId('5495f2a88766017d44130bb6') }, { 'published': 0, '_id': 0 }).sort('url'))) @mongo_data(users=[{ '_id': ObjectId('5495f2a88766017d44130bb6'), 'email': '*****@*****.**', 'nickname': 'james', 'password': bcrypt.generate_password_hash('password', rounds=12) }], bookmarks=[{ 'url': 'http://www.foo.com', 'user': { '_id': ObjectId('5495f2a88766017d44130bb6'), 'email': '*****@*****.**', 'nickname': 'james' }, 'published': datetime.datetime.now() }, { 'url': 'http://www.bar.com', 'user': { '_id': ObjectId('5495f2a88766017d44130bb6'), 'email': '*****@*****.**', 'nickname': 'james' }, 'published': datetime.datetime.now() }, { 'url': 'http://www.foo.com', 'user': { '_id': ObjectId('5495f2a88766017d44130bb1'), 'email': '*****@*****.**', 'nickname': 'tony' }, 'published': datetime.datetime.now() }]) def test_change_nickname(self): self.client.post('/', data=dict(email='*****@*****.**', password='******')) response = self.client.post('/profil/', data=dict(nickname='tony', email='*****@*****.**')) self.assert200(response) self.assertTemplateUsed('profil/index.html') self.assertEqual({ 'email': '*****@*****.**', 'nickname': 'tony' }, mongo.db.users.find_one({'email': '*****@*****.**'}, { 'password': 0, '_id': 0 })) self.assertTrue( bcrypt.check_password_hash( mongo.db.users.find_one({'email': '*****@*****.**'}, {'password': 1})['password'], 'password')) self.assertEqual( [{ 'url': 'http://www.bar.com', 'user': { '_id': ObjectId('5495f2a88766017d44130bb6'), 'email': '*****@*****.**', 'nickname': 'tony' } }, { 'url': 'http://www.foo.com', 'user': { '_id': ObjectId('5495f2a88766017d44130bb6'), 'email': '*****@*****.**', 'nickname': 'tony' } }], list( mongo.db.bookmarks.find( { 'user._id': ObjectId('5495f2a88766017d44130bb6') }, { 'published': 0, '_id': 0 }).sort('url'))) @mongo_data(users=[{ '_id': ObjectId('5495f2a88766017d44130bb6'), 'email': '*****@*****.**', 'nickname': 'james', 'password': bcrypt.generate_password_hash('password', rounds=12) }], bookmarks=[{ 'url': 'http://www.foo.com', 'user': { '_id': ObjectId('5495f2a88766017d44130bb6'), 'email': '*****@*****.**', 'nickname': 'james' }, 'published': datetime.datetime.now() }, { 'url': 'http://www.bar.com', 'user': { '_id': ObjectId('5495f2a88766017d44130bb6'), 'email': '*****@*****.**', 'nickname': 'james' }, 'published': datetime.datetime.now() }, { 'url': 'http://www.foo.com', 'user': { '_id': ObjectId('5495f2a88766017d44130bb1'), 'email': '*****@*****.**', 'nickname': 'tony' }, 'published': datetime.datetime.now() }]) def test_change_all_values(self): self.client.post('/', data=dict(email='*****@*****.**', password='******')) response = self.client.post('/profil/', data=dict(nickname='tony', email='*****@*****.**', actual_password='******', new_password='******', confirm_new_password='******')) self.assert200(response) self.assertTemplateUsed('profil/index.html') self.assertEqual({ 'email': '*****@*****.**', 'nickname': 'tony' }, mongo.db.users.find_one({'email': '*****@*****.**'}, { 'password': 0, '_id': 0 })) self.assertTrue( bcrypt.check_password_hash( mongo.db.users.find_one({'email': '*****@*****.**'}, {'password': 1})['password'], 'secret')) self.assertEqual( [{ 'url': 'http://www.bar.com', 'user': { '_id': ObjectId('5495f2a88766017d44130bb6'), 'email': '*****@*****.**', 'nickname': 'tony' } }, { 'url': 'http://www.foo.com', 'user': { '_id': ObjectId('5495f2a88766017d44130bb6'), 'email': '*****@*****.**', 'nickname': 'tony' } }], list( mongo.db.bookmarks.find( { 'user._id': ObjectId('5495f2a88766017d44130bb6') }, { 'published': 0, '_id': 0 }).sort('url')))