def test_delete_user(self): """Test for delete user function.""" guid = add_user(**self.user_to_add) self.assertIsNotNone(guid) delete_user(guid) user = get_user(guid) self.assertEqual(user, None)
def test_add_user(self): """Test for add user function.""" number_of_users_before = len(get_all_users()) guid = add_user(**self.user_to_add) self.assertIsNotNone(guid) number_of_users_after = len(get_all_users()) self.assertEqual(number_of_users_before + 1, number_of_users_after)
def test_get_user(self): """Test for getting user function.""" guid = add_user(**self.user_to_add) self.assertIsNotNone(guid) user = get_user(None) assert user is None user = get_user(guid) self.assertEqual('Akbar', user['name'])
def test_edit_user_view(self): """Test the edit_user_view function. """ guid = add_user(**self.correct_user_data) url = "/edit/%s" % guid try: return self.app.get(url, data=dict(), follow_redirects=True) except Exception, e: LOGGER.exception("Basic front page load failed.") raise e
def test_edit_user_view(self): """Test the edit_user_view function. """ guid = add_user(**self.correct_user_data) url = '/edit/%s' % guid try: return self.app.get(url, data=dict(), follow_redirects=True) except Exception, e: LOGGER.exception('Basic front page load failed.') raise e
def test_users_view(self): """Test the users json response works.""" guid = add_user(**self.correct_user_data) if guid is not None: try: result = self.app.post("/users.json", data=dict(user_role=1), follow_redirects=True) data = result.__getattribute__("data") self.assertTrue("Akbar" in data) except Exception, e: LOGGER.exception("Basic front page load failed.") raise e
def test_delete_user_view(self): """Test the delete_user_view function. """ guid = add_user(**self.correct_user_data) url = '/delete/%s' % guid try: self.app.post(url, data=dict(), follow_redirects=True) user = get_user(guid) self.assertIsNone(user) except Exception, e: LOGGER.exception('Basic front page load failed.') raise e
def test_delete_user_view(self): """Test the delete_user_view function. """ guid = add_user(**self.correct_user_data) url = "/delete/%s" % guid try: self.app.post(url, data=dict(), follow_redirects=True) user = get_user(guid) self.assertIsNone(user) except Exception, e: LOGGER.exception("Basic front page load failed.") raise e
def test_edit_user_controller(self): """Test the edit_user_view function. """ guid = add_user(**self.correct_user_data) self.edited_user_data["guid"] = guid url = "/edit_user" try: result = self.app.post(url, data=self.edited_user_data, follow_redirects=True) data = result.__getattribute__("data") self.assertTrue("Akbar Gumbira" in data) except Exception, e: LOGGER.exception("Basic front page load failed.") raise e
def test_users_view(self): """Test the users json response works.""" guid = add_user(**self.correct_user_data) if guid is not None: try: result = self.app.post('/users.json', data=dict(user_role=1), follow_redirects=True) data = result.__getattribute__('data') self.assertTrue('Akbar' in data) except Exception, e: LOGGER.exception('Basic front page load failed.') raise e
def test_edit_user_controller(self): """Test the edit_user_view function. """ guid = add_user(**self.correct_user_data) self.edited_user_data['guid'] = guid url = '/edit_user' try: result = self.app.post(url, data=self.edited_user_data, follow_redirects=True) data = result.__getattribute__('data') self.assertTrue('Akbar Gumbira' in data) except Exception, e: LOGGER.exception('Basic front page load failed.') raise e
def test_reminder_view(self): """Test the download_view function. """ url = "/reminder" # Test OK guid = add_user(**self.correct_user_data) if guid is not None: email = self.correct_user_data["email"] try: result = self.app.post(url, data=dict(email=email), follow_redirects=True) data = result.__getattribute__("data") self.assertTrue("Success" in data) except Exception, e: LOGGER.exception("Basic front page load failed.") raise e
def test_edit_user(self): """Test for edit user function.""" guid = add_user(**self.user_to_add) edited_data = dict(name='Akbar Gumbira', email='*****@*****.**', website='http://www.akbargumbira.com', role=2, email_updates='true', latitude=-6.32, longitude=102.03) guid = edit_user(guid, **edited_data) user = get_user(guid) for key in edited_data: if key != 'email_updates': self.assertEqual(edited_data[key], user[key]) self.assertEqual(user['email_updates'], 1)
def test_edit_user(self): """Test for edit user function.""" guid = add_user(**self.user_to_add) edited_data = dict( name='Akbar Gumbira', email='*****@*****.**', website='http://www.akbargumbira.com', role=2, email_updates='true', latitude=-6.32, longitude=102.03) guid = edit_user(guid, **edited_data) user = get_user(guid) for key in edited_data: if key != 'email_updates': self.assertEqual(edited_data[key], user[key]) self.assertEqual(user['email_updates'], 1)
def test_reminder_view(self): """Test the download_view function. """ url = '/reminder' # Test OK guid = add_user(**self.correct_user_data) if guid is not None: email = self.correct_user_data['email'] try: result = self.app.post(url, data=dict(email=email), follow_redirects=True) data = result.__getattribute__('data') self.assertTrue('Success' in data) except Exception, e: LOGGER.exception('Basic front page load failed.') raise e
def add_user_view(): """Controller to add a user. Handle post request via ajax and add the user to the user.db :returns: A new json response as in users.json. :rtype: HttpResponse .. note:: JavaScript on client must update the map on ajax completion callback. """ # return any errors as json - see http://flask.pocoo.org/snippets/83/ for code in default_exceptions.iterkeys(): APP.error_handler_spec[None][code] = make_json_error # Get data from form name = str(request.form['name']).strip() email = str(request.form['email']).strip() website = str(request.form['website']) role = int(request.form['role']) email_updates = str(request.form['email_updates']) latitude = str(request.form['latitude']) longitude = str(request.form['longitude']) # Validate the data: message = {} if not is_required_valid(name): message['name'] = 'Name is required' if not is_email_address_valid(email): message['email'] = 'Email address is not valid' if not is_required_valid(email): message['email'] = 'Email is required' if role not in [0, 1, 2]: message['role'] = 'Role must be checked' elif not is_boolean(email_updates): message['email_updates'] = 'Notification must be checked' # Check if the email has been registered by other user: user = get_user_by_email(email) if user is not None: message['email'] = 'Email has been registered by other user.' # Process data if len(message) != 0: message['type'] = 'Error' return Response(json.dumps(message), mimetype='application/json') else: # Modify the data: if email_updates == 'true': email_updates = True else: email_updates = False if len(website.strip()) != 0 and 'http' not in website: website = 'http://%s' % website # Create model for user and add user guid = add_user( name=name, email=email, website=website, role=int(role), email_updates=bool(email_updates), latitude=float(latitude), longitude=float(longitude)) # Prepare json for added user added_user = get_user(guid) # Send Email Confirmation: subject = '%s User Map Registration' % APP.config['PROJECT_NAME'] #noinspection PyUnresolvedReferences body = render_template( 'text/registration_confirmation_email.txt', project_name=APP.config['PROJECT_NAME'], url=APP.config['PUBLIC_URL'], user=added_user) recipient = added_user['email'] send_async_mail( sender=MAIL_ADMIN, recipients=[recipient], subject=subject, text_body=body, html_body='') #noinspection PyUnresolvedReferences added_user_json = render_template('json/users.json', users=[added_user]) # Return Response return Response(added_user_json, mimetype='application/json')
def test_get_user_by_email(self): """Test for getting user function.""" guid = add_user(**self.user_to_add) self.assertIsNotNone(guid) user = get_user_by_email(self.user_to_add['email']) self.assertEqual('Akbar', user['name'])
def add_user_view(): """Controller to add a user. Handle post request via ajax and add the user to the user.db :returns: A new json response as in users.json. :rtype: HttpResponse .. note:: JavaScript on client must update the map on ajax completion callback. """ # return any errors as json - see http://flask.pocoo.org/snippets/83/ for code in default_exceptions.iterkeys(): APP.error_handler_spec[None][code] = make_json_error # Get data from form name = str(request.form['name']).strip() email = str(request.form['email']).strip() website = str(request.form['website']) role = int(request.form['role']) email_updates = str(request.form['email_updates']) latitude = str(request.form['latitude']) longitude = str(request.form['longitude']) # Validate the data: message = {} if not is_required_valid(name): message['name'] = 'Name is required' if not is_email_address_valid(email): message['email'] = 'Email address is not valid' if not is_required_valid(email): message['email'] = 'Email is required' if role not in [0, 1, 2]: message['role'] = 'Role must be checked' elif not is_boolean(email_updates): message['email_updates'] = 'Notification must be checked' # Check if the email has been registered by other user: user = get_user_by_email(email) if user is not None: message['email'] = 'Email has been registered by other user.' # Process data if len(message) != 0: message['type'] = 'Error' return Response(json.dumps(message), mimetype='application/json') else: # Modify the data: if email_updates == 'true': email_updates = True else: email_updates = False if len(website.strip()) != 0 and 'http' not in website: website = 'http://%s' % website # Create model for user and add user guid = add_user(name=name, email=email, website=website, role=int(role), email_updates=bool(email_updates), latitude=float(latitude), longitude=float(longitude)) # Prepare json for added user added_user = get_user(guid) # Send Email Confirmation: subject = '%s User Map Registration' % APP.config['PROJECT_NAME'] #noinspection PyUnresolvedReferences body = render_template('text/registration_confirmation_email.txt', project_name=APP.config['PROJECT_NAME'], url=APP.config['PUBLIC_URL'], user=added_user) recipient = added_user['email'] send_async_mail(sender=MAIL_ADMIN, recipients=[recipient], subject=subject, text_body=body, html_body='') #noinspection PyUnresolvedReferences added_user_json = render_template('json/users.json', users=[added_user]) # Return Response return Response(added_user_json, mimetype='application/json')