def setUp(cls): """Provide the base for the tests.""" super(EventHandlerTest, cls).setUp() methods = set(cls.webapp2.WSGIApplication.allowed_methods) methods.add('PATCH') cls.webapp2.WSGIApplication.allowed_methods = frozenset(methods) app = cls.webapp2.WSGIApplication([("/api/events/(.*)", EventHandler)], debug=True) cls.testapp = cls.webtest.TestApp(app) """Init the models.""" # new User cls.user = mocks.create_user('*****@*****.**') # new User cls.second_user = mocks.create_user('*****@*****.**') # new Institution cls.institution = mocks.create_institution() cls.institution.members = [cls.user.key] cls.institution.followers = [cls.user.key] cls.institution.admin = cls.user.key cls.institution.put() """ Update User.""" cls.user.add_institution(cls.institution.key) cls.user.follows = [cls.institution.key] cls.user.put() # Events cls.event = mocks.create_event(cls.user, cls.institution)
def test_get_one_event(self, verify_token): """Test the institution_events_handler's get method.""" institution = mocks.create_institution() user = mocks.create_user('*****@*****.**') event = mocks.create_event(user, institution) # Call the get method events = self.testapp.get("/api/institutions/%s/events?page=0&limit=1" % institution.key.urlsafe()) # Retrieve the entity event_retrieved = (events.json['events'])[0] key_event = ndb.Key(urlsafe=event_retrieved['key']) event_obj = key_event.get() #Check the conditions self.assertEqual(event_obj.title, event.title, "The titles must be equal") self.assertEqual(event_obj.start_time, event.start_time, "The start_time must be equal") self.assertEqual(event_obj.end_time, event.end_time, "The end_time must be equal") self.assertEqual(event_obj.author_key, user.key, "The authors must be the same") self.assertEqual(event_obj.institution_key, institution.key, "The institutions must be the same")
def test_get_many_events(self, verify_token): """Test the institution_events_handler's get method.""" institution = mocks.create_institution() user = mocks.create_user('*****@*****.**') event = mocks.create_event(user, institution) second_event = mocks.create_event(user, institution) third_event = mocks.create_event(user, institution) # Call the get method events = self.testapp.get( "/api/institutions/%s/events?page=0&limit=3" % institution.key.urlsafe()) # Retrieve the entity events_retrieved = (events.json['events']) keys = [obj['key'] for obj in events_retrieved] #Check the final conditions self.assertTrue(event.key.urlsafe() in keys) self.assertTrue(second_event.key.urlsafe() in keys) self.assertTrue(third_event.key.urlsafe() in keys) self.assertTrue(len(keys) == 3)
def test_post_shared_event(self, verify_token, send_message_notification, enqueue_task): """Test the post_collection_handler's post method in case that post is shared_event.""" # create an event event = mocks.create_event(self.other_user, self.institution) event.text = "Description of new Event" event.put() # Make the request and assign the answer to post self.body['post'] = { 'institution': self.institution.key.urlsafe(), 'shared_event': event.key.urlsafe() } post = self.testapp.post_json("/api/posts", self.body, headers={ 'institution-authorization': self.institution.key.urlsafe() }).json # Retrieve the entities key_post = ndb.Key(urlsafe=post['key']) post_obj = key_post.get() self.institution = self.institution.key.get() self.user = self.user.key.get() # Check class object self.assertEqual(post_obj._class_name(), 'Post', "The class of post is 'Post'") # Check if the post's key is in institution and user self.assertTrue(key_post in self.user.posts, "The post is not in user.posts") # Check if the post's attributes are the expected self.assertEqual(post_obj.institution, self.institution.key, "The post's institution is not the expected one") shared_event_obj = post['shared_event'] # Check if the shared_post's attributes are the expected self.assertEqual(shared_event_obj['title'], event.title, "The post's title expected is New Event") self.assertEqual( shared_event_obj['institution_key'], self.institution.key.urlsafe(), "The post's institution expected is %s" % self.institution.key.urlsafe()) self.assertEqual( shared_event_obj['author_key'], self.other_user.key.urlsafe(), "The post's institution expected is %s" % self.other_user.key.urlsafe()) self.assertEqual(shared_event_obj['text'], event.text, "The post's text expected is %s" % event.text) calls = [ call( "add-post-institution", { 'institution_key': self.institution.key.urlsafe(), 'created_post_key': post_obj.key.urlsafe() }), call( 'notify-followers', { 'sender_key': self.user.key.urlsafe(), 'entity_key': key_post.urlsafe(), 'entity_type': 'POST', 'institution_key': self.institution.key.urlsafe(), 'current_institution': self.institution.key.urlsafe() }) ] message = { "from": { "photo_url": self.user.photo_url, "name": self.user.name, "institution_name": event.institution_key.get().name }, "to": { "institution_name": "" }, "current_institution": { "name": self.institution.name } } # assert the notifiction was sent to the institution followers send_message_notification.assert_called_with( receiver_key=event.author_key.urlsafe(), notification_type="SHARED_EVENT", entity_key=key_post.urlsafe(), message=json.dumps(message)) # assert that add post to institution was sent to the queue enqueue_task.assert_has_calls(calls)