Skip to content

Commit

Permalink
feature: add test
Browse files Browse the repository at this point in the history
  • Loading branch information
abbas123456 committed Feb 4, 2021
1 parent 1fa57d4 commit 8ec7427
Show file tree
Hide file tree
Showing 8 changed files with 345 additions and 28 deletions.
107 changes: 80 additions & 27 deletions core/app/feeds.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ def parse_feed_config(feed_config):
by_feed_type = {
'activity_stream': ActivityStreamFeed,
'zendesk': ZendeskFeed,
'maxemail': EventFeed,
'maxemail': MaxemailFeed,
'aventri': EventFeed,
}
return by_feed_type[feed_config['TYPE']].parse_config(feed_config)
Expand Down Expand Up @@ -333,13 +333,20 @@ async def fetch_attendees(event_id):
continue

attendee_object = {
'type': ['Attendee', 'dit:aventri:Attendee'],
'id': 'dit:aventri:Attendee:' + attendee['attendeeid'],
'registration_status': attendee['registrationstatus'],
'individual_cost': attendee['individualcost'],
'cost': attendee['cost'],
'total_cost': attendee['totalcost'],
'created': attendee['created']
'published': datetime.datetime.strptime(
attendee['created'], '%Y-%m-%d %H:%M:%S'
).isoformat(),
'type': ['Attendee', 'dit:aventri:Attendee'],
'dit:aventri:approvalstatus': attendee['approvalstatus'],
'dit:aventri:category': attendee['category']['name'],
'dit:aventri:createdby': attendee['createdby'],
'dit:aventri:language': attendee['language'],
'dit:aventri:lastmodified': attendee['lastmodified'],
'dit:aventri:modifiedby': attendee['modifiedby'],
'dit:aventri:registrationstatus': attendee['registrationstatus'],
'dit:aventri:responses': [{'name': r['name'], 'response': r['response']}
for r in attendee['responses'].values()]
}
attendees.append(attendee_object)
return attendees
Expand All @@ -365,43 +372,89 @@ async def fetch_event(event_id):
headers={'accesstoken': self.access_token})
result.raise_for_status()
event = json_loads(result._body)
if 'error' in event:
event = {}
else:
event['attendeees'] = await fetch_attendees(event_id)

if 'error' not in event:
event['attendees'] = await fetch_attendees(event_id)

await context.redis_client.execute(
'SETEX', f'event-{event_id}', 60*60*24, json_dumps(event))
return event

now = datetime.datetime.now().isoformat()
# pylint: disable=bad-continuation
return [
{
'type': 'Create',
'id': 'dit:aventri:Event:' + str(event['eventid']) + ':Create',
'published': now,
'eventid': event['eventid'],
'type': 'dit:aventri:Event',
'dit:application': 'aventri',
'object': {
'type': ['Event', 'dit:aventri:Event'],
'attributedTo': event['attendees'],
'id': 'dit:aventri:Event:' + event['eventid'],
'name': event['name'],
'url': event['url'],
'content': event['description'],
'startdate': event['startdate'],
'enddate': event['enddate'],
'foldername': event['foldername'],
'location': event['location'],
'language': event['defaultlanguage'],
'timezone': event['timezone'],
'currency': event['standardcurrency'],
'price_type': event['price_type'],
'price': event['pricepoints'],
'attributedTo': event['attendees']
'published': datetime.datetime.strptime(
event['createddatetime'], '%Y-%m-%d %H:%M:%S'
).isoformat(),
'type': ['Event', 'dit:aventri:Event'],

# The following keys are not namespaced with aventri in order for the mappings
# to be available for queries in great.gov.uk search.
# see https://readme.trade.gov.uk/docs/playbooks/activity-stream/structure.html
'dit:description': event['description'],
'dit:foldername': event['foldername'],
'dit:include_calendar': event['include_calendar'],
'dit:status': event['status'],
'dit:url': event['url'],

'dit:aventri:approval_required': event['approval_required'],
'dit:aventri:approval_status': event['approval_status'],
'dit:aventri:city': event['city'],
'dit:aventri:clientcontact': event['clientcontact'],
'dit:aventri:closedate': event['closedate'],
'dit:aventri:closetime': event['closetime'],
'dit:aventri:code': event['code'],
'dit:aventri:contactinfo': event['contactinfo'],
'dit:aventri:country': event['country'],
'dit:aventri:createdby': event['createdby'],
'dit:aventri:defaultlanguage': event['defaultlanguage'],
'dit:aventri:enddate': event['enddate'],
'dit:aventri:endtime': event['endtime'],
'dit:aventri:folderid': event['folderid'],
'dit:aventri:live_date': event['live_date'],
'dit:aventri:location_address1': event['location']['address1']
if event['location'] else None,
'dit:aventri:location_address2': event['location']['address2']
if event['location'] else None,
'dit:aventri:location_address3': event['location']['address3']
if event['location'] else None,
'dit:aventri:location_city': event['location']['city']
if event['location'] else None,
'dit:aventri:location_country': event['location']['country']
if event['location'] else None,
'dit:aventri:location_name': event['location']['name']
if event['location'] else None,
'dit:aventri:location_postcode': event['location']['postcode']
if event['location'] else None,
'dit:aventri:location_state': event['location']['state']
if event['location'] else None,
'dit:aventri:locationname': event['locationname'],
'dit:aventri:login1': event['login1'],
'dit:aventri:login2': event['login2'],
'dit:aventri:max_reg': event['max_reg'],
'dit:aventri:modifiedby': event['modifiedby'],
'dit:aventri:modifieddatetime': event['modifieddatetime'],
'dit:aventri:price_type': event['price_type'],
'dit:aventri:pricepoints': event['pricepoints'],
'dit:aventri:standardcurrency': event['standardcurrency'],
'dit:aventri:startdate': event['startdate'],
'dit:aventri:starttime': event['starttime'],
'dit:aventri:state': event['state'],
'dit:aventri:timezone': event['timezone'],
}
}
for page_event in feed
for event in [await get_event(page_event['eventid'])]
if event is not None
if 'eventid' in event
]


Expand Down
2 changes: 1 addition & 1 deletion core/app/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ def json_loads(data):
def main(run_application_coroutine):
stdout_handler = logging.StreamHandler(sys.stdout)
app_logger = logging.getLogger('activity-stream')
app_logger.setLevel(logging.DEBUG)
app_logger.setLevel(logging.INFO)
app_logger.addHandler(stdout_handler)

loop = asyncio.get_event_loop()
Expand Down
56 changes: 56 additions & 0 deletions core/tests/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -1791,6 +1791,62 @@ def has_two_zendesk_tickets(results):
self.assertIn('"dit:zendesk:Ticket:3:Create"', results)
self.assertIn('"2011-04-12T12:48:13+00:00"', results)

@async_test
async def test_aventri(self):
def aventri_fetch(results):
if 'hits' not in results or 'hits' not in results['hits']:
return False
aventri_events = [
item
for item in results['hits']['hits']
for source in [item['_source']]
if 'dit:application' in source and source['dit:application'] == 'aventri'
]
return len(aventri_events) == 1

env = {
**mock_env(),
'FEEDS__1__UNIQUE_ID': 'third_feed',
'FEEDS__1__ACCOUNT_ID': '1234',
'FEEDS__1__API_KEY': '5678',
'FEEDS__1__TYPE': 'aventri',
'FEEDS__1__SEED': 'http://localhost:8081/tests_fixture_aventri_listEvents.json',
'FEEDS__1__AUTH_URL': 'http://localhost:8081/tests_fixture_aventri_authorize.json',
'FEEDS__1__EVENT_URL': 'http://localhost:8081/tests_fixture_aventri_getEvent.json',
'FEEDS__1__ATTENDEES_LIST_URL':
'http://localhost:8081/tests_fixture_aventri_listAttendees.json',
'FEEDS__1__ATTENDEE_URL':
'http://localhost:8081/tests_fixture_aventri_getAttendee.json',
}

with patch('asyncio.sleep', wraps=fast_sleep):
await self.setup_manual(env=env, mock_feed=read_file, mock_feed_status=lambda: 200,
mock_headers=lambda: {})
results_dict = await fetch_all_es_data_until(aventri_fetch)

event = results_dict['hits']['hits'][0]['_source']

self.assertEqual(event['dit:application'], 'aventri')
self.assertEqual(event['id'], 'dit:aventri:Event:1:Create')
self.assertEqual(event['type'], 'dit:aventri:Event')

self.assertEqual(event['object']['id'], 'dit:aventri:Event:1')
self.assertEqual(event['object']['type'], ['Event', 'dit:aventri:Event'])
self.assertEqual(event['object']['name'], 'Demo Event')
self.assertEqual(event['object']['published'], '2021-01-27T00:00:00')
self.assertEqual(event['object']['dit:aventri:location_city'], 'London')

self.assertEqual(event['object']['attributedTo'][0]['id'],
'dit:aventri:Attendee:1')
self.assertEqual(event['object']['attributedTo'][0]['type'],
['Attendee', 'dit:aventri:Attendee'])
self.assertEqual(event['object']['attributedTo'][0]['published'],
'2014-10-06T14:10:01')
self.assertEqual(event['object']['attributedTo'][0]['dit:aventri:category'],
'Event Speaker')
self.assertEqual(event['object']['attributedTo'][0]['dit:aventri:responses'][0],
{'name': 'Email Address', 'response': 'test@example.com'})

@async_test
async def test_maxemail(self):
def maxemail_base_fetch(results):
Expand Down
3 changes: 3 additions & 0 deletions core/tests/tests_fixture_aventri_authorize.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"accesstoken": "sampletoken_juqGzTXYWBrqLAWMYGukNzuvOD5TXTGxxMo0lSft69q2MTgfodwQXRvlsx1YHExTM7Dd4mu5bucbrr3I2isCisrQieie"
}
64 changes: 64 additions & 0 deletions core/tests/tests_fixture_aventri_getAttendee.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
{
"adjustments": "0.00",
"agendacost": "0.00",
"approvalstatus": "",
"attendeeid": "1",
"balancedue": "0.00",
"category": {
"categoryid": "1",
"name": "Event Speaker"
},
"categorycost": "0.00",
"cost": "0.00",
"created": "2014-10-06 14:10:01",
"createdby": "attendee",
"guestcost": "0.00",
"hotelcost": "0.00",
"individualcost": "0.00",
"language": "eng",
"last_lobby_login": "2020-10-30 06:08:30",
"lastmodified": "2014-10-06 14:10:31",
"modifiedby": "attendee",
"optioncost": "0.00",
"preload-recordid": "1",
"received": "0.00",
"recordid": "XXX",
"registrationstatus": "Confirmed",
"responses": {
"1": {
"choicekey": "test@example.com",
"fieldname": "email",
"name": "Email Address",
"page": "Welcome",
"pageid": "1",
"questionid": "1",
"response": "test@example.com"
},
"2": {
"auto_capitalize": "0",
"choicekey": "1",
"fieldname": "28341900",
"name": "Session A",
"page": "Agenda",
"pageid": "3",
"questionid": "28341900",
"response": "Session A"
},
"3": {
"auto_capitalize": "0",
"choicekey": "1",
"fieldname": "28341982",
"name": "Option A",
"page": "Options",
"pageid": "4",
"questionid": "28341982",
"response": "Option A"
}
},
"statetax": "0.00",
"subcategorycost": "0.00",
"tax": "0.00",
"testregistration": "0",
"totalcost": "0.00",
"virtual_event_attendance": "Yes"
}

0 comments on commit 8ec7427

Please sign in to comment.