Commit 2a4712a8 authored by Nicholas J Anderson's avatar Nicholas J Anderson
Browse files

Merge branch 'ci' into 'master'

Implements CI into Peoplefinder API

Additionally, the ldap functionality has been expanded.

See merge request !3
parents f8ad70c5 76022e82
Pipeline #407 passed with stage
in 3 minutes and 45 seconds
test:
script:
- apt-get update -qy
- apt-get install -y python-dev python-pip libsasl2-dev python-dev libldap2-dev libssl-dev
- pip install -r requirements.txt
- python -m pytest test.py --cov peoplefinder.py --cov app.py
from flask import Flask, jsonify, render_template
from peoplefinder import call_standard, call_ldap
from peoplefinder import call_standard, ldap_get, ldap_getAll, ldap_search, ldap_query
import simpleldap
app = Flask(__name__)
conn = simpleldap.Connection('directory.gmu.edu')
@app.route('/')
def base_api():
......@@ -13,7 +15,7 @@ def search_all(search, page):
standardResults = call_standard(search, page=page)
if len(standardResults['results']) < 1:
try:
res=jsonify(call_ldap(search))
res=jsonify(ldap_get(conn, search))
except Exception as e:
print(e)
return res
......@@ -30,9 +32,21 @@ def search_students(search, page):
def search_faculty(search, page):
return jsonify(call_standard(search, group="faculty", page=page))
@app.route('/ldap/<search>')
@app.route('/ldap/get/<search>')
def get_ldap(search):
return jsonify(ldap_get(conn, search))
@app.route('/ldap/getall/<search>')
def many_ldap(search):
return jsonify(ldap_getAll(conn, search))
@app.route('/ldap/search/<search>')
def search_ldap(search):
return jsonify(call_ldap(search))
return jsonify(ldap_search(conn, search))
@app.route('/ldap/query/<search>')
def query_ldap(search):
return jsonify(ldap_query(conn, search))
if __name__ == "__main__":
import os
......
......@@ -57,19 +57,34 @@ def call_standard(search, group="all", page=1):
'method': 'peoplefinder'
}
def call_ldap(search):
conn = simpleldap.Connection('directory.gmu.edu')
res = conn.get("uid=%s" % search)
return {
"results": [
{
"name": res['cn'][0],
"mail": res['mail'][0],
"surname": res['sn'][0],
"givenname": res['givenName'][0],
"dn": res.dn
}
],
"hasNextPage": False,
"method": "ldap"
}
\ No newline at end of file
def build_ldap_response(conn_res):
response = {
'results': [],
'hasNextPage': False,
'method': 'ldap'
}
if not isinstance(conn_res, list):
conn_res = [conn_res]
for person in conn_res:
response['results'].append({
"name": person.get('cn', [""])[0],
"mail": person.get('mail', [""])[0],
"surname": person.get('sn', [""])[0],
"givenname": person.get('givenName', [""])[0],
})
return response
def ldap_get(conn, search):
return build_ldap_response(conn.get("uid=%s" % search))
def ldap_getAll(conn, search):
return build_ldap_response(conn.search("uid=%s" % search))
def ldap_search(conn, search):
return build_ldap_response(conn.search("(|(uid={0})(sn={0})(givenName={0}))".format(search)))
def ldap_query(conn, search):
return build_ldap_response(conn.search(search))
......@@ -7,3 +7,7 @@ beautifulsoup4==4.3.2
itsdangerous==0.24
requests==2.5.1
wsgiref==0.1.2
simpleldap==0.8
pytest==3.0.3
pytest-cov==2.4.0
import os
import app as web
import peoplefinder as API
import unittest
import tempfile
from pprint import pprint
import simpleldap
conn = simpleldap.Connection("directory.gmu.edu")
## Cache a set of peoplefinder responses
pf_sample1 = API.call_standard("nand")
pf_sample2 = API.call_standard("kdobolyi")
ldap_sample1 = API.ldap_get(conn, "kdobolyi")
ldap_sample2 = API.ldap_getAll(conn, "nander*")
ldap_sample3 = API.ldap_search(conn, "Kinga")
ldap_sample4 = API.ldap_query(conn, "(&(givenName=Kinga L)(sn=Dobolyi))")
class APITestCase(unittest.TestCase):
def setUp(self):
web.app.config['TESTING'] = True
self.app = web.app.test_client()
with web.app.app_context():
pass
def test_pf_paging(self):
self.assertTrue(pf_sample1['hasNextPage'])
self.assertFalse(pf_sample2['hasNextPage'])
def test_pf_method(self):
self.assertEqual(pf_sample1['method'], 'peoplefinder')
self.assertEqual(pf_sample2['method'], 'peoplefinder')
def test_pf_results(self):
self.assertTrue(len(pf_sample1['results']) > 5)
self.assertTrue(len(pf_sample2['results']) == 1)
def test_pf_has_result(self):
res = False
for obj in pf_sample1['results']:
if obj.get('name', '') == u"Shenandoah Neighborhood Housing Desk":
res = True
self.assertTrue(res)
self.assertEqual(pf_sample2['results'][0]['name'], u"Dobolyi, Kinga")
def test_ldap_paging(self):
self.assertFalse(ldap_sample1['hasNextPage'])
self.assertFalse(ldap_sample2['hasNextPage'])
self.assertFalse(ldap_sample3['hasNextPage'])
self.assertFalse(ldap_sample4['hasNextPage'])
def test_ldap_method(self):
self.assertEqual(ldap_sample1['method'], 'ldap')
self.assertEqual(ldap_sample2['method'], 'ldap')
self.assertEqual(ldap_sample3['method'], 'ldap')
self.assertEqual(ldap_sample4['method'], 'ldap')
def test_ldap_results_len(self):
self.assertEqual(len(ldap_sample1['results']), 1)
self.assertGreater(len(ldap_sample2['results']), 5)
self.assertGreater(len(ldap_sample3['results']), 1)
self.assertEqual(len(ldap_sample4['results']), 1)
if __name__ == '__main__':
unittest.main()
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment