Skip to content

Commit

Permalink
Merge pull request #72 from kelockhart/myads-disable
Browse files Browse the repository at this point in the history
Added endpoint to enable/disable all myADS for a user
  • Loading branch information
kelockhart committed May 30, 2023
2 parents 8f2061e + cc713fb commit 777687c
Show file tree
Hide file tree
Showing 3 changed files with 133 additions and 1 deletion.
2 changes: 1 addition & 1 deletion requirements.txt
@@ -1,4 +1,4 @@
git+https://github.com/adsabs/ADSMicroserviceUtils.git@v1.1.9
git+https://github.com/adsabs/ADSMicroserviceUtils.git@v1.2.3
git+https://github.com/adsabs/ADSParser.git@v1.0.4
alembic==0.8.9
psycopg2==2.8.6
Expand Down
94 changes: 94 additions & 0 deletions vault_service/tests/test_user.py
Expand Up @@ -775,5 +775,99 @@ def test_myads_import(self):
self.assertEqual(len(r.json['new']), 1)
self.assertEqual(len(r.json['existing']), 0)

@httpretty.activate
def test_myads_status_update(self):
with self.app.session_scope() as session:
q = session.query(Query).first()

qid = q.qid

# make sure no setups exist
r = self.client.get(url_for('user.myads_notifications'),
headers={'Authorization': 'secret', 'X-Adsws-Uid': '33'})

self.assertStatus(r, 204)

# this shouldn't do anything since there are no setups
r = self.client.put(url_for('user.myads_status', user_id=33),
headers={'Authorization': 'secret'},
data=json.dumps({'active': False}),
content_type='application/json')

self.assertStatus(r, 204)

# save some queries
r = self.client.post(url_for('user.myads_notifications'),
headers={'Authorization': 'secret', 'X-Adsws-Uid': '33'},
data=json.dumps({'name': 'Query 1', 'qid': qid, 'stateful': True,
'frequency': 'daily', 'type': 'query'}),
content_type='application/json')

self.assertStatus(r, 200)
self.assertTrue(r.json['active'])

r = self.client.post(url_for('user.myads_notifications'),
headers={'Authorization': 'secret', 'X-Adsws-Uid': '33'},
data=json.dumps({'name': 'Query 2', 'qid': qid, 'stateful': True,
'frequency': 'daily', 'type': 'query'}),
content_type='application/json')

self.assertStatus(r, 200)
self.assertTrue(r.json['active'])

# send a bad request
r = self.client.put(url_for('user.myads_status', user_id=33),
headers={'Authorization': 'secret'},
data=json.dumps({'stateful': False}),
content_type='application/json')

self.assertStatus(r, 400)

# try to send extra keys
r = self.client.put(url_for('user.myads_status', user_id=33),
headers={'Authorization': 'secret'},
data=json.dumps({'frequency': 'weekly', 'active': True}),
content_type='application/json')

self.assertStatus(r, 200)

r = self.client.get(url_for('user.myads_notifications'),
headers={'Authorization': 'secret', 'X-Adsws-Uid': '33'})
self.assertStatus(r, 200)

for setup in r.json:
# status should be unchanged
self.assertTrue(setup['active'])
# frequency should also be unchanged
self.assertEqual(setup['frequency'], 'daily')

# disable all notifications
r = self.client.put(url_for('user.myads_status', user_id=33),
headers={'Authorization': 'secret'},
data=json.dumps({'active': False}),
content_type='application/json')

self.assertStatus(r, 200)

r = self.client.get(url_for('user.myads_notifications'),
headers={'Authorization': 'secret', 'X-Adsws-Uid': '33'})

for setup in r.json:
self.assertFalse(setup['active'])

# enable all notifications
r = self.client.put(url_for('user.myads_status', user_id=33),
headers={'Authorization': 'secret'},
data=json.dumps({'active': True}),
content_type='application/json')

self.assertStatus(r, 200)

r = self.client.get(url_for('user.myads_notifications'),
headers={'Authorization': 'secret', 'X-Adsws-Uid': '33'})

for setup in r.json:
self.assertTrue(setup['active'])

if __name__ == '__main__':
unittest.main()
38 changes: 38 additions & 0 deletions vault_service/views/user.py
Expand Up @@ -806,6 +806,44 @@ def export(iso_datestring):
return json.dumps({'users': output}), 200


@advertise(scopes=['ads-consumer:myads'], rate_limit = [1000, 3600*24])
@bp.route('/myads-status-update/<user_id>', methods=['PUT'])
def myads_status(user_id):
"""
Enable/disable all myADS notifications for a given user
payload: {'active': True/False}
:param user_id: ID of user to update
:return:
"""
try:
payload, headers = check_request(request)
except Exception as e:
return json.dumps({'msg': e.message or e.description}), 400


if 'active' in payload:
status = {'active': payload['active']}
else:
return 'Only status updates allowed', 400

with current_app.session_scope() as session:
all_setups = session.query(MyADS).filter_by(user_id=user_id).order_by(MyADS.id.asc()).all()
if len(all_setups) == 0:
return '{}', 204
ids = [s.id for s in all_setups]

for s in ids:
msg, status_code = _edit_myads_notification(status, headers, user_id, s)
if status_code != 200:
outmsg = 'Error %s while updating status for setup %s for user %s. Error message: %s',\
status_code, s, user_id, msg
return json.dumps({'msg': outmsg}), 500

return '{}', 200


@advertise(scopes=[], rate_limit=[1000, 3600*24])
@bp.route('/myads-import', methods=['GET'])
def import_myads():
Expand Down

0 comments on commit 777687c

Please sign in to comment.