def test_validate_email_already_exists(context): User.create_user('test', '*****@*****.**', 'test') is_valid, err_msg = validate_email_already_exists('*****@*****.**') is_valid.should.equal(False) err_msg.should.equal('Email already exists.') is_valid, err_msg = validate_email_already_exists('*****@*****.**') is_valid.should.equal(True) err_msg.should.equal('')
def test_delete_by_email(context): user = User.create_user('ramadan', '*****@*****.**', 'pass1234') User.delete_by_email('wrong').should.equal((False, {})) User.delete_by_email('*****@*****.**').should.equal((True, { 'first_name': 'ramadan', 'email': '*****@*****.**', 'uuid': user.uuid, })) User.query.filter_by(email='*****@*****.**').first().should.equal(None)
def add_user(): if request.method == "GET": return render_template('cms_addcmsuser.html') else: form = AddUserForm(request.form) if form.validate(): login_name = form.login_name.data name = form.name.data user = User(login_name=login_name,name=name) user.password = '******' db.session.add(user) db.session.commit() return Api.success() else: return Api.params_error(form.get_error)
def test_api_list_users(context): ("GET on /api/users list all users") User.create_user('ramadan2', '*****@*****.**', 'pass12344') User.create_user('ramadan2', '*****@*****.**', 'pass12344') response = context.http.get("/api/users") response.status_code.should.equal(200) data = json.loads(response.data) data.should.equal([{ 'first_name': 'ramadan2', 'email': '*****@*****.**', 'uuid': '13fcf48d5e398eda1f6ec6531c927ab2' }, { 'first_name': 'ramadan2', 'email': '*****@*****.**', 'uuid': 'f9558794ccfab66c8b49f17c93eeae8e' }])
def test_api_update_user_email_already_exists(context): ("PUT on /api/user/<uuid> email already exists") User.create_user('ramadan2', '*****@*****.**', 'pass12344') user = User.create_user('ramadan2', '*****@*****.**', 'pass12344') response = context.http.put( "/api/user/{}".format(user.uuid), data=json.dumps({ 'first_name': 'ramadan3', 'email': '*****@*****.**', 'password': '******' }), content_type='application/json', ) response.status_code.should.equal(400) data = json.loads(response.data) data.should.have.key("email").being.equal(['Email already exists.'])
def test_to_dict(context): user = User.create_user('ramadan', '*****@*****.**', 'pass1234') user.to_dict().should.equal({ 'first_name': 'ramadan', 'email': '*****@*****.**', 'uuid': user.uuid, })
def test_update_user(context): user = User.create_user('ramadan', '*****@*****.**', 'pass1234') user.update_user('ramadan1', '*****@*****.**', 'pass11234') User.query.filter_by(email='*****@*****.**').first().should.equal(None) updated_user = User.query.filter_by(email='*****@*****.**').first() updated_user.first_name.should.equal('ramadan1') updated_user.password.should.equal(get_md5('pass11234'))
def test_api_create_user_email_exists(context): ("POST on /api/user email already exists") User.create_user('ramadan2', '*****@*****.**', 'pass12344') response = context.http.post( "/api/user", data=json.dumps({ 'first_name': 'ramadan', 'email': '*****@*****.**', 'password': '******' }), content_type='application/json', ) response.headers.should.have.key("Content-Type").being.equal( "application/json") response.status_code.should.equal(400) data = json.loads(response.data) data.should.have.key("email").being.equal(['Email already exists.'])
def test_api_retrieve_user_valid(context): ("GET on /api/user/<uuid> with valid uuid") user = User.create_user('ramadan2', '*****@*****.**', 'pass12344') response = context.http.get("/api/user/{}".format(user.uuid)) response.status_code.should.equal(200) data = json.loads(response.data) data.should.have.key("first_name").being.equal(user.first_name) data.should.have.key("email").being.equal(user.email) data.should.have.key("uuid").being.equal(user.uuid)
def test_api_delete_user_valid_email(context): ("DELETE on /api/user/<email> with valid email ") user = User.create_user('ramadan2', '*****@*****.**', 'pass12344') response = context.http.delete("/api/user/{}".format(user.email)) response.status_code.should.equal(200) data = json.loads(response.data) data.should.have.key("first_name").being.equal(user.first_name) data.should.have.key("email").being.equal(user.email) data.should.have.key("uuid").being.equal(user.uuid) User.query.filter_by( email='*****@*****.**').first().should.equal(None)
def test_api_update_user_password_valid(context): ("PUT on /api/user/<uuid> update user password") user = User.create_user('ramadan2', '*****@*****.**', 'pass12344') response = context.http.put( "/api/user/{}".format(user.uuid), data=json.dumps({'password': '******'}), content_type='application/json', ) response.status_code.should.equal(200) data = json.loads(response.data) data.should.have.key("first_name").being.equal('ramadan2') data.should.have.key("email").being.equal('*****@*****.**') data.should.have.key("uuid").being.equal(user.uuid)
def api_create_user(): request_data = request.get_json() or {} first_name = request_data.get('first_name') email = request_data.get('email', '') password = request_data.get('password') validators = [ {'field_name': 'first_name', 'func': validate_field_required, 'field_val': first_name}, {'field_name': 'email', 'func': validate_field_required, 'field_val': email}, {'field_name': 'password', 'func': validate_field_required, 'field_val': password}, {'field_name': 'email', 'func': validate_email_format, 'field_val': email}, {'field_name': 'email', 'func': validate_email_already_exists, 'field_val': email}, ] errors = run_validators(validators) if errors: return json_response(errors, 400) user = User.create_user(first_name, email, password) return json_response(user.to_dict(), 200)
def api_user_delete_by_email(email): is_deleted, data = User.delete_by_email(email) if not is_deleted: return json_response({}, 404) return json_response(data, 200)
def test___repr__(context): user = User.create_user('ramadan', '*****@*****.**', 'pass1234') repr(user).should.equal('*****@*****.**')