def test_util_create_subscriptions(self): """Test the create subscriptions utility function.""" #Create the test subscriptions subscriber_id = 'TESTSUBSCRIBERID' util.create_subscriptions( subscriber_id, self.subscriber['topics'] ) #For each topic subscription create, check that a subscription exists for the right subscriber for topic in self.subscriber['topics']: query_response = self.subscriptions.query( IndexName='topicID-index', KeyConditionExpression=Key('topicID').eq(topic) ) self.assertEquals( len(query_response['Items']), 1 ) self.assertEquals( query_response['Items'][0]['subscriberID'], subscriber_id ) #Delete the subscriptions so the database is kept clean. with self.subscriptions.batch_writer() as batch: for item in query_response['Items']: batch.delete_item( Key={ 'subscriptionID': item['subscriptionID'] } )
def get(self, subscriber_id): """ Creates the subscriptions and sets the subscriber's "verified" attribute to True. Args: subscriber_id (str): The ID for the subscriber who has been verified. Returns: A json object with attribute "message" informing whether the verificaiton was successful e.g. {"message":"Subscriber verified"} """ #Check subscriberID doesn't exist in subscriptions already exists = self.subscriptions.query( IndexName='subscriberID-index', KeyConditionExpression=Key('subscriberID').eq(subscriber_id) ) #Get subscriber details. subscriber = self.subscribers.get_item( Key={ 'id':subscriber_id } ) if not (exists['Items'] or subscriber['Item']['verified']): topics = subscriber['Item']['topics'] #Create the subscriptions util.create_subscriptions( subscriber_id, topics ) #Update the verified field and delete the code attribute. self.subscribers.update_item( Key={ 'id': subscriber_id }, AttributeUpdates={ 'verified':{ 'Value':True }, 'code':{ 'Action':'DELETE' } } ) message = { "message": "Subscriber verified" } return Response( json.dumps( message ), status=200, mimetype='application/json' ) else: message = { "message":"400 Bad Request: Subscriber has already been verified." } return Response( json.dumps( message ), status=400, mimetype='application/json' )
def put(self): """ Add a new subscriber. Parse the given arguments to check it is a valid subscriber. Assign the subscriber a uuid in hex that is used to identify the subscriber when wishing to delete it. Does not use the subscriber_id argument. Arguments are passed in the request data. Args: first_name (str): Required. The subscriber's first name.\n last_name (str): Required. The subscriber's last name.\n email (str): Required. The subscriber's email address.\n country (str): Required. The country that the subscriber has signed up to.\n sms (str): The subscribers phone number for sms.\n topics ([str]): Required. The ID's for the topics to which the subscriber wishes to subscribe.\n verified (bool): Are their contact details verified? Defaults to False. Returns: The amazon dynamodb response, with the assigned subscriber_id added. """ #Define an argument parser for creating a new subscriber. parser = reqparse.RequestParser() parser.add_argument('first_name', required=True, type=str, help='First name of the subscriber') parser.add_argument('last_name', required=True, type=str, help='Last name of the subscriber') parser.add_argument('email', required=True, type=str, help='Email address of the subscriber') parser.add_argument('country', required=True, type=str, help='Country subscribed to') parser.add_argument('sms', required=False, type=str, help='Mobile phone number of the subscriber') parser.add_argument('verified', required=False, type=bool, help='Are the contact details verified?') parser.add_argument('topics', action='append', required=True, type=str, help='List of topic IDs the subscriber wishes to subscribe to') args = parser.parse_args() subscriber_id = uuid.uuid4().hex subscriber = { 'id': subscriber_id, 'first_name': args['first_name'], 'last_name': args['last_name'], 'country': args['country'], 'email': args['email'], 'topics': args['topics'] } if args['sms'] is not None: subscriber['sms'] = args['sms'] if args['verified'] is not None: subscriber['verified'] = args['verified'] else: subscriber['verified'] = False response = self.subscribers.put_item( Item=subscriber ) response['subscriber_id'] = subscriber_id if subscriber['verified']: util.create_subscriptions( subscriber_id, args['topics'] ) return Response( json.dumps( response ), status=response['ResponseMetadata']['HTTPStatusCode'], mimetype='application/json' )