def test_link_orcid_auth_callback(name, request_ctx): """Test ORCID callback - the user authorized the organisation access to the ORCID profile.""" with request_ctx("/auth") as ctx: org = Organisation(name="THE ORGANISATION", confirmed=True) org.save() test_user = User( name=name, email="*****@*****.**", username="******", organisation=org, orcid="ABC123", confirmed=True) login_user(test_user, remember=True) rv = ctx.app.full_dispatch_request() assert rv.status_code == 302, "If the user is already affiliated, the user should be redirected ..." assert "profile" in rv.location, "redirection to 'profile' showing the ORCID" u = User.get(username="******") assert u.orcid == "ABC-123-456-789" assert u.access_token == "ABC123" if name: assert u.name == name, "The user name should be changed" else: assert u.name == "NEW TEST", "the user name should be set from record coming from ORCID"
def test_reset_db(request_ctx): """Test reset_db function for 'testing' cycle reset.""" with request_ctx("/reset_db") as ctx: org = Organisation(name="THE ORGANISATION") org.save() u = User(email="*****@*****.**", name="TEST USER", username="******", roles=Role.SUPERUSER, orcid=None, confirmed=True) u.save() root = User(email="*****@*****.**", name="The root", username="******", roles=Role.SUPERUSER, orcid=None, confirmed=True) root.save() assert User.select().count() == 2 assert Organisation.select().count() == 1 login_user(u, remember=True) rv = ctx.app.full_dispatch_request() assert User.select().count() == 1 assert Organisation.select().count() == 0 assert rv.status_code == 302 assert rv.location == url_for("logout")
def get_org(self): base_url = self.url("orgs/" + self.organisation) response = requests.get(base_url, headers=self.github_headers()) if response.status_code == 404: return {} response.raise_for_status() data = response.json() mapping = [ ("login", "login"), ("description", "description"), ("nom", "name"), ("organisation_url", "html_url"), ("avatar_url", "avatar_url"), ("site_web", "blog"), ("adresse", "location"), ("email", "email"), ("est_verifiee", "is_verified"), ("nombre_repertoires", "public_repos"), ("date_creation", "created_at"), ] current_dict = {} for key, json_key in mapping: try: current_dict[key] = self.clean_data(data[json_key], key) except KeyError: current_dict[key] = None current_dict["plateforme"] = "GitHub" return Organisation(**current_dict)
def test_tuakiri_login_with_org(client): """ Test logging attempt via Shibboleth. If a user logs in from an organisation that isn't onboared, the user should be informed about that and redirected to the login page. """ org = Organisation(name="THE ORGANISATION") org.save() rv = client.get("/Tuakiri/login", headers={ "Auedupersonsharedtoken": "ABC111", "Sn": "LAST NAME/SURNAME/FAMILY NAME", 'Givenname': "FIRST NAME/GIVEN NAME", "Mail": "*****@*****.**", "O": "THE ORGANISATION", "Displayname": "TEST USER FROM THE ORGANISATION" }, follow_redirects=True) u = User.get(email="*****@*****.**") assert u.organisation == org assert org in u.organisations assert u.edu_person_shared_token == "ABC111" assert b"Your organisation (INCOGNITO) is not onboarded" not in rv.data uo = UserOrg.get(user=u, org=org) assert not uo.is_admin
def get_org(self): url = self.base_url + "groups/" + self.organisation response = requests.get(url) if response.status_code == 404: return {} response.raise_for_status() data = response.json() res = { "login": data["path"], "description": data["description"], "nom": data["name"], "organisation_url": data["web_url"], "avatar_url": self.avatar_url(data["avatar_url"]), "site_web": None, "adresse": None, "email": None, "est_verifiee": None, "nombre_repertoires": len(data["projects"]), "date_creation": None, "plateforme": "GitLab", } return Organisation(**res)
def organisation_basic(): data = request.get_json(force=True) name = data['name'] desc = data['desc'] organisation = Organisation(name=name, desc=desc) session.add(organisation) session.commit() return jsonify({ 'status': "registered", 'name': organisation.name, 'desc': organisation.desc })
def test_profile_wo_orcid(request_ctx): """Test a user profile that doesn't hava an ORCID.""" with request_ctx("/profile") as ctx: org = Organisation(name="THE ORGANISATION", confirmed=True) org.save() test_user = User( email="*****@*****.**", username="******", organisation=org, orcid=None, confirmed=True) login_user(test_user, remember=True) rv = ctx.app.full_dispatch_request() assert rv.status_code == 302 assert rv.location == url_for("link")
def test_profile(request_ctx): """Test an affilated user profile and ORCID data retrieval.""" with request_ctx("/profile") as ctx: org = Organisation(name="THE ORGANISATION", confirmed=True) org.save() test_user = User( email="*****@*****.**", username="******", organisation=org, orcid="ABC123", confirmed=True) login_user(test_user, remember=True) rv = ctx.app.full_dispatch_request() assert rv.status_code == 200 assert b"TEST1234567890" in rv.data
def test_link(request_ctx): """Test a user affiliation initialization.""" with request_ctx("/link") as ctx: org = Organisation(name="THE ORGANISATION", confirmed=True) org.save() test_user = User( name="TEST USER 123", email="*****@*****.**", username="******", organisation=org, confirmed=True) login_user(test_user, remember=True) rv = ctx.app.full_dispatch_request() assert b"<!DOCTYPE html>" in rv.data, "Expected HTML content" assert b"TEST USER 123" in rv.data, "Expected to have the user name on the page" assert b"*****@*****.**" in rv.data, "Expected to have the user email on the page" assert b"URL_123" in rv.data, "Expected to have ORCiD authorization link on the page"
def test_link_already_affiliated(request_ctx): """Test a user affiliation initialization if the uerer is already affilated.""" with request_ctx("/link") as ctx: org = Organisation(name="THE ORGANISATION", confirmed=True) org.save() test_user = User( email="*****@*****.**", name="TEST USER", username="******", organisation=org, orcid="ABC123", confirmed=True) test_user.save() login_user(test_user, remember=True) uo = UserOrg(user=test_user, org=org) uo.save() rv = ctx.app.full_dispatch_request() assert rv.status_code == 302, "If the user is already affiliated, the user should be redirected ..." assert "profile" in rv.location, "redirection to 'profile' showing the ORCID"
def invite_organisation(): """Invite an organisation to register. Flow: * Hub administrort (super user) invokes the page, * Fills in the form with the organisation and organisation technica contatct data (including an email address); * Submits the form; * A secure registration token gets ceated; * An email message with confirmation link gets created and sent off to the technical contact. """ form = OrgRegistrationForm() if request.method == "POST": if not form.validate(): flash("Please fill in all fields and try again.", "danger") else: email = form.orgEmailid.data org_name = form.orgName.data try: User.get(User.email == form.orgEmailid.data) flash( "This Email address is already an Admin for one of the organisation", "warning") except User.DoesNotExist: pass finally: # TODO: organisation can have mutiple admins: # TODO: user OrgAdmin try: org = Organisation.get(name=org_name) # TODO: fix it! org.email = email except Organisation.DoesNotExist: org = Organisation(name=org_name, email=email) org.save() try: user = User.get(email=email) user.roles |= Role.ADMIN user.organisation = org except User.DoesNotExist: user = User( name=form.orgName.data, email=form.orgEmailid.data, confirmed=True, # In order to let the user in... roles=Role.ADMIN, organisation=org) user.save() # Note: Using app context due to issue: https://github.com/mattupstate/flask-mail/issues/63 with app.app_context(): msg = Message("Welcome to OrcidhHub", recipients=[str(form.orgEmailid.data)]) token = generate_confirmation_token(form.orgEmailid.data) # TODO: do it with templates msg.body = "Your organisation is just one step behind to get onboarded" \ " please click on following link to get onboarded " \ "https://" + environ.get("ENV", "dev") + ".orcidhub.org.nz" + \ url_for("confirm_organisation", token=token) mail.send(msg) flash( "Organisation Onboarded Successfully!!! Email Communication has been sent to Admin", "success") return render_template('registration.html', form=form)
import models from models import Organisation, User, Role models.drop_talbes() models.create_tables() org0 = Organisation(name="The Royal Society of NewZealand", email="*****@*****.**", tuakiri_name="The Royal Society of NewZealand", orcid_client_id="client-123", orcid_secret="secret-123", confirmed=True) org0.save() super_user = User(name="The Royal Society of NewZealand", email="*****@*****.**", edu_person_shared_token="aaRtDix1l2z43M0vvWTBpBuf_ek", confirmed=True, roles=Role.SUPERUSER) super_user.save() super_user = User(name="The Root", email="*****@*****.**", confirmed=True, roles=Role.SUPERUSER) super_user.save()
def post(self, *args, **kwargs): """Register an orgsanition""" if request.is_json: current_user = get_current_user() if not current_user: return jsonify({ 'status': False, 'msg': 'User doesn\'t exist', 'data': None }), 404 if db.session.query(user_organisation_table). \ filter(user_organisation_table.c.user_id == current_user.id).first(): return jsonify({ 'status': False, 'msg': 'A user can be associated with only one organisation', 'data': None }), 400 request_data = request.get_json() if Organisation.query.filter_by( name=request_data.get('name')).first(): return jsonify({ 'status': False, 'msg': 'Organisation already exists', 'data': None }), 400 serializer = OrganisationRegistrationSerializer() try: validated_request_data = serializer.load(request_data) validated_request_data['passcode'] = generate_password_hash( password=validated_request_data.pop('passcode')) validated_request_data['registered_by'] = current_user.id organisation = Organisation(**validated_request_data) organisation.user_organisation.append(current_user) db.session.add(organisation) db.session.commit() return jsonify({ 'status': True, 'msg': 'Organisation registation successful', 'data': { 'id': organisation.id } }), 201 except ValidationError as err: return jsonify({ 'status': False, 'msg': 'Validation failed', 'data': None, 'errors': err.messages }), 400 return jsonify({ 'status': False, 'msg': 'Invalid JSON', 'data': None }), 400
def reset_db_with_fixtures(db=db): db.drop_all() db.create_all() #----------------------------------------------------------------------------# # Fixtures - organisation #----------------------------------------------------------------------------# org0 = Organisation( auth0_id='auth0|60c58135612d820070a5f049', name='Test Organisation', description='Test organisation authenticated through Auth0', website='http://mywebsite.com', phone_contact='1111111', email_contact='*****@*****.**') org0.insert() org1 = Organisation(name='Pet Welfare Society', description='Open your heart to a cat in need', website='https://www.catwelfare.org/', email_contact='*****@*****.**', phone_contact='96111111') org1.insert() org2 = Organisation( name='East Youths', description='An organisation of youths for the community', website='eastyouths.org', email_contact='*****@*****.**') org2.insert() #----------------------------------------------------------------------------# # Fixtures - users #----------------------------------------------------------------------------# u0 = User(auth0_id='auth0|60c58174612d820070a5f057', name='Test User', age=17, email_contact='*****@*****.**', phone_contact='1111111', join_date=datetime(2020, 5, 21, 21, 30, 0), skills=['cooking', 'web development']) u0.insert() u1 = User(name='User01', age=31, email_contact='*****@*****.**', phone_contact='1111111', join_date=datetime(2020, 7, 1, 0, 0, 0), skills=['counselling']) u1.insert() u2 = User(name='User02', age=45, email_contact='*****@*****.**', phone_contact='1111111', join_date=datetime(2019, 12, 12, 0, 0, 0), skills=None) u2.insert() u3 = User(name='User03', age=28, email_contact='*****@*****.**', phone_contact='1111111', join_date=datetime(2020, 12, 12, 0, 0, 0), skills=['counselling']) u3.insert() #----------------------------------------------------------------------------# # Fixtures - Events #----------------------------------------------------------------------------# e0 = Event(name='test event 0', description='this is a test event', start_datetime=datetime(2021, 1, 12, 10, 0, 0), end_datetime=datetime(2021, 1, 12, 12, 0, 0), address='London SW1A 0AA, UK', organisation=org0, participants=[u0, u1]) e0.insert() e1 = Event(name='test event 1', description='this is a test event', start_datetime=datetime(2021, 1, 12, 17, 0, 0), end_datetime=datetime(2021, 1, 12, 18, 0, 0), address='London SW1A 0AA, UK', organisation=org1, participants=[u0]) e1.insert() e2 = Event(name='test event 2', description='this is a test event', start_datetime=datetime(2021, 3, 1, 10, 0, 0), end_datetime=datetime(2021, 3, 1, 12, 0, 0), address='London SW1A 0AA, UK', organisation=org2, participants=[u1, u3]) e2.insert() e3 = Event(name='test event 3', description='this is a test event', start_datetime=datetime(2021, 4, 1, 10, 0, 0), end_datetime=datetime(2021, 4, 1, 12, 0, 0), address='London SW1A 0AA, UK', organisation=org2, participants=[u0, u1, u3]) e3.insert() e4 = Event(name='test event 4', description='this is a test event', start_datetime=datetime(2021, 5, 1, 10, 0, 0), end_datetime=datetime(2021, 5, 1, 12, 0, 0), address='London SW1A 0AA, UK', organisation=org2, participants=[u0, u2, u3]) e4.insert()
def generate_base_organisation(name: str) -> Organisation: org = Organisation(name=name) db.session.add(org) db.session.commit() return org