def test_get(self, verify_token): """Test the get method.""" # create models # new User user = mocks.create_user('*****@*****.**') # new Institution FIRST INST first_inst = mocks.create_institution('FIRST INST') first_inst.state = "active" first_inst.put() # new Institution SECOND INST with pending state default second_inst = mocks.create_institution('SECOND INST') # new Institution THIRD INST third_inst = mocks.create_institution('THIRD INST') third_inst.state = "active" third_inst.put() # Call the get method all_institutions = self.testapp.get("/api/institutions?page=0&&limit=2").json self.assertEqual(len(all_institutions), 2, "The length of all institutions list should be 2") self.assertEqual(all_institutions['institutions'][0]['name'], first_inst.name, "The name of institituion should be equal to the first_inst name") self.assertEqual(all_institutions['institutions'][1]['name'], third_inst.name, "The name of institution should be equal to the third_inst name")
def setUp(cls): """Provide the base for the tests.""" super(UserRequestCollectionHandlerTest, cls).setUp() app = cls.webapp2.WSGIApplication( [(UserRequestCollectionHandlerTest.REQUEST_URI, UserRequestCollectionHandler)], debug=True) cls.testapp = cls.webtest.TestApp(app) # create models # new User cls.user_admin = mocks.create_user('*****@*****.**') # new institution cls.other_inst = mocks.create_institution() # Other user cls.other_user = mocks.create_user('*****@*****.**') cls.other_user.institutions = [cls.other_inst.key] cls.other_user.put() # new Institution inst test cls.inst_test = mocks.create_institution() cls.inst_test.name = 'inst test' cls.inst_test.photo_url = 'www.photo.com' cls.inst_test.members = [cls.user_admin.key] cls.inst_test.followers = [cls.user_admin.key] cls.inst_test.admin = cls.user_admin.key cls.user_admin.add_institution(cls.inst_test.key) cls.inst_test.put() # create header cls.headers = { "Institution-Authorization": cls.other_inst.key.urlsafe() }
def get_all_parent_admins(self): """Test get_all_parent_admins method.""" first_user = mocks.create_user() second_user = mocks.create_user() third_user = mocks.create_user() first_inst = mocks.create_institution() second_inst = mocks.create_institution() third_inst = mocks.create_institution() fourth_inst = mocks.create_institution() first_inst.add_member(first_user) first_inst.set_admin(first_user.key) third_inst.add_member(first_user) third_inst.set_admin(first_user.key) second_inst.add_member(second_user) second_inst.set_admin(second_user.key) fourth_inst.add_member(third_user) fourth_inst.set_admin(third_user.key) first_user.institutions_admin.append(first_inst.key) first_user.institutions_admin.append(third_inst.key) second_user.institutions_admin.append(second_inst.key) third_user.institutions_admin.append(fourth_inst.key) # Hierarchy # first_inst -> second_inst -> third_inst -> fourth_inst # (first_user) (second_user) (first_user) (third_user) second_inst.parent_institution = first_inst.key third_inst.parent_institution = second_inst.key fourth_inst.parent_institution = third_inst.key first_inst.children_institutions.append(second_inst.key) third_inst.children_institutions.append(fourth_inst.key) first_inst.put() second_inst.put() third_inst.put() fourth_inst.put() first_user.put() second_user.put() third_user.put() admins = get_all_parent_admins(fourth_inst, []) self.assertTrue(second_user not in admins) self.assertEquals([first_user, third_user], admins) second_inst.children_institutions.append(third_inst.key) admins = get_all_parent_admins(fourth_inst, []) self.assertTrue(second_user in admins) self.assertEquals([first_user, second_user, third_user], admins)
def test_get_institution(self, verify_token): """Test the search_handler's get method.""" splab = mocks.create_institution() splab.name = 'Software Practice Laboratory' splab.acronym = 'SPLAB' splab.state = 'pending' splab.actuation_area = 'Universidades' splab.put() certbio = mocks.create_institution() certbio.name = 'Laboratório de Avaliação e Desenvolvimento de Biomateriais do Nordeste' certbio.acronym = 'CERTBIO' certbio.state = 'active' certbio.actuation_area = 'Universidades' certbio.description = 'Ensaio Químico - Determinação de Material Volátil por Gravimetria...' certbio.put() # Search for the institution by its full name institutions = self.testapp.get( "/api/search/institution?value=%s&state=%s&type=institution" % (certbio.name, 'active')) self.assertTrue('CERTBIO' in institutions) # Search for the institution by part of its name institutions = self.testapp.get( "/api/search/institution?value=%s&state=%s&type=institution" % ('Biomateriais', 'active')) self.assertTrue('CERTBIO' in institutions) # Search for the institution by its acronym institutions = self.testapp.get( "/api/search/institution?value=%s&state=%s&type=institution" % ('CERTBIO', 'active')) self.assertTrue('CERTBIO' in institutions) # Make sure that there is no institution CERTBIO with pending state. institutions = self.testapp.get( "/api/search/institution?value=%s&state=%s&type=institution" % ('CERTBIO', 'pending')) self.assertTrue('CERTBIO' not in institutions) # Make sure that there is no SPLAB with pending state. institutions = self.testapp.get( "/api/search/institution?value=%s&state=%s&type=institution" % ('SPLAB', 'active')) self.assertTrue('SPLAB' not in institutions) # Assert that SPLAB has a pending state institutions = self.testapp.get( "/api/search/institution?value=%s&state=%s&type=institution" % ('SPLAB', 'pending')) self.assertTrue('SPLAB' in institutions) # Search for institutions by its actuation area institutions = self.testapp.get( "/api/search/institution?value=%s&state=%s&type=institution" % ('Universidades', 'active, pending')) self.assertTrue('CERTBIO' and 'SPLAB' in institutions) # Search for institutions by description institutions = self.testapp.get( "/api/search/institution?value=%s&state=%s&type=institution" % ('Ensaio quimico', 'active')) self.assertTrue('CERTBIO' in institutions)
def test_delete_child_connection(self, verify_token, send_message_notification): """Test delete method with isParent=false.""" # Assert the initial conditions admin = mocks.create_user(ADMIN['email']) otheruser = mocks.create_user(USER['email']) institution = mocks.create_institution() otherinst = mocks.create_institution() institution.children_institutions.append(otherinst.key) otherinst.parent_institution = institution.key institution.admin = admin.key otherinst.admin = otheruser.key institution.put() otherinst.put() admin.add_institution(institution.key) admin.add_permission("remove_link", institution.key.urlsafe()) admin.put() self.assertTrue(otherinst.key in institution.children_institutions) self.assertTrue(otherinst.parent_institution == institution.key) # Call the delete method self.testapp.delete( "/api/institutions/%s/hierarchy/%s?isParent=false" % (institution.key.urlsafe(), otherinst.key.urlsafe()), headers={'institution-authorization': institution.key.urlsafe()} ) # Update the institutions institution = institution.key.get() otherinst = otherinst.key.get() # Assert the final conditions self.assertTrue( otherinst.key not in institution.children_institutions) self.assertTrue(otherinst.parent_institution == institution.key) message = { "from": { "photo_url": admin.photo_url, "name": admin.name, "institution_name": institution.name }, "to": { "institution_name": otherinst.name }, "current_institution": { "name": institution.name } } # assert the notification was sent send_message_notification.assert_called_with( receiver_key=otheruser.key.urlsafe(), notification_type="REMOVE_INSTITUTION_LINK", entity_key=otherinst.key.urlsafe(), message=json.dumps(message) )
def test_post(self, verify_token): """Test method post of InstitutionParentRequestCollectionHandler.""" admin = mocks.create_user(ADMIN['email']) institution = mocks.create_institution() admin.institutions_admin = [institution.key] institution.admin = admin.key admin.add_permission("send_link_inst_request", institution.key.urlsafe()) admin.put() institution.put() other_user = mocks.create_user(USER['email']) inst_requested = mocks.create_institution() admin_requested = mocks.create_user(ADMIN['email']) admin.institutions_admin = [inst_requested.key] admin_requested.put() inst_requested.admin = admin_requested.key inst_requested.put() data = { 'sender_key': admin.key.urlsafe(), 'is_request': True, 'admin_key': admin_requested.key.urlsafe(), 'institution_key': institution.key.urlsafe(), 'institution_requested_key': inst_requested.key.urlsafe(), 'type_of_invite': 'REQUEST_INSTITUTION_PARENT' } body = { 'data': data, 'currentInstitution': { 'name': 'currentInstitution' } } request = self.testapp.post_json( "/api/institutions/" + institution.key.urlsafe() + "/requests/institution_parent", data) request = json.loads(request._app_iter[0]) institution = institution.key.get() self.assertEqual(request['sender'], admin.email, 'Expected sender email is [email protected]') self.assertEqual(request['admin_name'], admin_requested.name, 'Expected sender admin_name is User Admin') self.assertEqual( request['type_of_invite'], 'REQUEST_INSTITUTION_PARENT', 'Expected sender type_of_invite is REQUEST_INSTITUTION_PARENT') self.assertEqual( institution.parent_institution, inst_requested.key, "The parent institution of inst test must be update to inst_requested" )
def setUp(cls): """Provide the base for the tests.""" super(InstitutionMemberHandlerTest, cls).setUp() app = cls.webapp2.WSGIApplication( [("/api/institutions/(.*)/members", InstitutionMembersHandler)], debug=True) cls.testapp = cls.webtest.TestApp(app) # create models # new User user cls.user = mocks.create_user('*****@*****.**') # new User second_user cls.second_user = mocks.create_user('*****@*****.**') # new Institution institution cls.institution = mocks.create_institution() cls.institution.members = [cls.user.key, cls.second_user.key] cls.institution.followers = [cls.user.key, cls.second_user.key] cls.institution.admin = cls.user.key cls.institution.put() # another institution cls.other_institution = mocks.create_institution() cls.other_institution.members = [cls.user.key] cls.other_institution.followers = [cls.user.key, cls.second_user.key] cls.other_institution.admin = cls.user.key cls.other_institution.put() # update user cls.user.institutions = [ cls.institution.key, cls.other_institution.key ] cls.user.follows = [cls.institution.key, cls.other_institution.key] cls.user.institutions_admin = [ cls.institution.key, cls.other_institution.key ] cls.user.add_permission("publish_post", cls.institution.key.urlsafe()) cls.user.add_permission("publish_post", cls.other_institution.key.urlsafe()) cls.user.add_permission("remove_member", cls.institution.key.urlsafe()) cls.user.add_permission("remove_member", cls.other_institution.key.urlsafe()) cls.user.put() cls.second_user.institutions = [cls.institution.key] cls.second_user.follows = [cls.institution.key] cls.second_user.add_permission("publish_post", cls.institution.key.urlsafe()) cls.second_user.put() # create headers cls.headers = { 'Institution-Authorization': cls.institution.key.urlsafe() }
def setUp(cls): """Provide the base for the tests.""" super(InviteUserHandlerTest, cls).setUp() methods = set(cls.webapp2.WSGIApplication.allowed_methods) methods.add('PATCH') cls.webapp2.WSGIApplication.allowed_methods = frozenset(methods) app = cls.webapp2.WSGIApplication([ (InviteUserHandlerTest.INVITE_USER_URI, InviteUserHandler), ], debug=True) cls.testapp = cls.webtest.TestApp(app) # create models # new User cls.user_admin = mocks.create_user('*****@*****.**') # Other user cls.other_user = mocks.create_user('*****@*****.**') # new Institution inst test cls.inst_test = mocks.create_institution() cls.inst_test.admin = cls.user_admin.key cls.inst_test.state = "active" cls.inst_test.put() # New invite user cls.data = { 'invitee': '*****@*****.**', 'admin_key': cls.user_admin.key.urlsafe(), 'institution_key': cls.inst_test.key.urlsafe(), 'type_of_invite': 'USER' } cls.invite = InviteUser.create(cls.data) cls.invite.put()
def test_post_invalid_invite_type(self, verify_token): """Test post invalid invite type.""" admin = mocks.create_user() other_user = mocks.create_user() institution = mocks.create_institution() institution.add_member(admin) institution.set_admin(admin.key) admin.add_permissions(DEFAULT_ADMIN_PERMISSIONS, institution.key.urlsafe()) admin.put() institution.put() invite_body = create_body([other_user.email[0]], admin, institution, 'USER') invite_body['data']['invite_body'][ 'type_of_invite'] = 'INSTITUION_CHILDREN' verify_token._mock_return_value = {'email': admin.email[0]} with self.assertRaises(Exception) as raises_context: self.testapp.post_json('/api/invites/user', invite_body, headers={ 'institution-authorization': institution.key.urlsafe() }) message = self.get_message_exception(str(raises_context.exception)) self.assertEqual( message, 'Error! invitation type not allowed', "Expected error message is Error! invitation type not allowed")
def setUp(cls): """Provide the base for the tests.""" super(PostCommentHandlerTest, cls).setUp() app = cls.webapp2.WSGIApplication( [("/api/posts/(.*)/comments/(.*)", PostCommentHandler), ("/api/posts/(.*)/comments", PostCommentHandler)], debug=True) cls.testapp = cls.webtest.TestApp(app) # Create models # new User cls.user = mocks.create_user(USER_EMAIL) # new User cls.other_user = mocks.create_user(OTHER_USER_EMAIL) # 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() cls.user.add_institution(cls.institution.key) cls.other_user.add_institution(cls.institution.key) # POST of user To institution cls.user_post = mocks.create_post(cls.user.key, cls.institution.key) # Comments cls.comment = { 'text': 'Frist comment. Using in Test', 'institution_key': cls.institution.key.urlsafe() } cls.other_comment = { 'text': 'Second comment. Using in Test', 'institution_key': cls.institution.key.urlsafe() } # http post body cls.body = {'commentData': cls.comment}
def setUp(cls): """Provide the base for the tests.""" super(LikeCommentHandlerTest, cls).setUp() app = cls.webapp2.WSGIApplication([ ("/api/posts/(.*)/comments/(.*)/likes", LikeHandler), ], debug=True) cls.testapp = cls.webtest.TestApp(app) # mocking entities # new Users cls.user = mocks.create_user('*****@*****.**') cls.other_user = mocks.create_user('*****@*****.**') cls.third_user = mocks.create_user('*****@*****.**') # new Institution cls.institution = mocks.create_institution() cls.user.add_institution(cls.institution.key) cls.other_user.add_institution(cls.institution.key) # Post of User cls.post = mocks.create_post(cls.user.key, cls.institution.key) # comment cls.comment = mocks.create_comment(cls.institution.key.urlsafe(), cls.third_user) # add comment to post cls.post.add_comment(cls.comment) cls.post = cls.post.key.get() # creating uri cls.uri = '/api/posts/%s/comments/%s/likes' % (cls.post.key.urlsafe(), cls.comment.id)
def setUp(cls): """Provide the base for the tests.""" super(ReplyCommentHandlerTest, cls).setUp() methods = set(cls.webapp2.WSGIApplication.allowed_methods) methods.add('PATCH') cls.webapp2.WSGIApplication.allowed_methods = frozenset(methods) app = cls.webapp2.WSGIApplication([ ("/api/posts/(.*)/comments/(.*)/replies", ReplyCommentHandler), ("/api/posts/(.*)/comments/(.*)/replies/(.*)", ReplyCommentHandler) ], debug=True) cls.testapp = cls.webtest.TestApp(app) # create models # new User cls.user = mocks.create_user(USER_EMAIL) # other user cls.other_user = mocks.create_user(OTHER_USER_EMAIL) # third user cls.third_user = mocks.create_user(THIRD_USER_EMAIL) # new Institution CERTBIO cls.institution = mocks.create_institution() cls.third_user.add_institution(cls.institution.key) # POST of User To Certbio Institution cls.user_post = mocks.create_post(cls.user.key, cls.institution.key) # comment from other_user cls.other_user_comment = mocks.create_comment( cls.institution.key.urlsafe(), cls.other_user) # reply from third_user cls.reply = mocks.create_comment(cls.institution.key.urlsafe(), cls.third_user) # add comment to post cls.user_post.add_comment(cls.other_user_comment) cls.user_post = cls.user_post.key.get() cls.user_post.put()
def setUp(cls): """Provide the base for the tests.""" super(InviteInstitutionCollectionHandlerTest, cls).setUp() app = cls.webapp2.WSGIApplication([ ("/api/invites/institution", InviteInstitutionCollectionHandler), ], debug=True) cls.testapp = cls.webtest.TestApp(app) # create models # new User cls.first_user = mocks.create_user('*****@*****.**') # new Institution cls.institution = mocks.create_institution() # set the institution admin to be the first user cls.first_user.institutions_admin = [cls.institution.key] cls.first_user.add_institution(cls.institution.key) cls.first_user.put() cls.institution.admin = cls.first_user.key cls.institution.add_member(cls.first_user) cls.institution.put() # update first user permissions cls.first_user.add_permission('send_invite_inst', cls.institution.key.urlsafe()) cls.first_user.put() data = { 'invitee': '*****@*****.**', 'admin_key': cls.first_user.key.urlsafe(), 'type_of_invite': 'INSTITUTION', 'suggestion_institution_name': 'New Institution', 'institution_key': cls.institution.key.urlsafe() } cls.body = {'data': data}
def test_delete_user_not_admin(self, verify_token): """Test delete request with user is not admin.""" admin = mocks.create_user(ADMIN['email']) institution = mocks.create_institution() institution.admin = admin.key institution.put() admin.institutions_admin = [institution.key] admin.add_permission("answer_user_request", institution.key.urlsafe()) admin.put() otheruser = mocks.create_user(USER['email']) data = { 'sender_key': otheruser.key.urlsafe(), 'is_request': True, 'admin_key': admin.key.urlsafe(), 'institution_key': institution.key.urlsafe(), 'type_of_invite': 'REQUEST_USER' } request = RequestUser.create(data) request.put() with self.assertRaises(Exception) as ex: self.testapp.delete('/api/requests/' + request.key.urlsafe() + '/user') exception_message = self.get_message_exception(ex.exception.message) self.assertEqual( "Error! User is not allowed to reject user request", exception_message, "Expected error message is Error! User is not allowed to reject user request" )
def teste_delete_usermember(self, verify_token): """Test that user member try unfollow the institution.""" user = mocks.create_user(USER['email']) institution = mocks.create_institution() user.follow(institution.key) user.add_institution(institution.key) institution.followers = [user.key] institution.add_member(user) self.assertEquals(len(institution.followers), 1, "The institution should have a follower") self.assertEquals(len(user.follows), 1, "The user should follow an institution") # Call the delete method self.testapp.delete("/api/institutions/%s/followers" % institution.key.urlsafe()) # Update the objects user = user.key.get() institution = institution.key.get() # Assert that the institution hasn't been removed from user.follows self.assertEquals(len(user.follows), 1, "The user should follow an institution") # Assert that the user hasn't been removed from the followers self.assertEquals(len(institution.followers), 1, "The institution should have a follower")
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 teste_delete_usermember_health_ministry(self, verify_token): """Test that user member try unfollow the health ministry institution.""" user = mocks.create_user(USER['email']) institution = mocks.create_institution() user.follow(institution.key) user.add_institution(institution.key) institution.followers = [user.key] institution.name = "Ministério da Saúde" institution.add_member(user) self.assertEquals(len(institution.followers), 1, "The institution should have a follower") self.assertEquals(len(user.follows), 1, "The user should follow an institution") # Call the delete method with self.assertRaises(Exception) as raises_context: self.testapp.delete("/api/institutions/%s/followers" % institution.key.urlsafe()) message_exception = self.get_message_exception( str(raises_context.exception)) self.assertEqual( message_exception, "Error! The institution can not be unfollowed", "Expected exception message must be equal to Error! The institution can not be unfollowed" )
def test_get(self, verify_token): """Test the institution_timeline_handler get method.""" user = mocks.create_user() verify_token._mock_return_value = {'email': user.email[0]} institution = mocks.create_institution() institution.address = mocks.create_address() user.add_institution(institution.key) institution.add_member(user) post = mocks.create_post(user.key, institution.key) post.last_modified_by = user.key post_aux = mocks.create_post(user.key, institution.key) post_aux.last_modified_by = user.key post.put() post_aux.put() # Call the get method posts = self.testapp.get("/api/institutions/%s/timeline?page=0&&limit=2" % institution.key.urlsafe()) # Update the objects post_top = (posts.json['posts'])[0] post_last = (posts.json['posts'])[1] # Verify if the posts was published and your informations self.assertEqual( post_top, post_aux.make(posts.request.host), "The maked post should be equal to the expected one" ) self.assertEqual( post_last, post.make(posts.request.host), "The maked post should be equal to the expected one" ) # Call the delete method for a post that has activity post = post.like(user.key) post.delete(user) # Call the get method posts = self.testapp.get("/api/institutions/%s/timeline?page=0&&limit=2" % institution.key.urlsafe()) # Update the objects post_top = (posts.json['posts'])[0] post_last = (posts.json['posts'])[1] # Verify if the post was deleted and your informations self.assertEqual(post_top["title"], None, "The title expected was null") self.assertEqual(post_top["text"], None, "The text expected was null") self.assertEqual(post_top["state"], 'deleted', "The state of post should be deleted") self.assertEqual( post_last, post_aux.make(posts.request.host), "The maked post should be equal to the expected one" )
def test_get_with_deleted_post(self, verify_token): """Test the institution_timeline_handler get method with deleted post.""" user = mocks.create_user() verify_token._mock_return_value = {'email': user.email[0]} institution = mocks.create_institution() user.add_institution(institution.key) institution.add_member(user) post = mocks.create_post(user.key, institution.key) post.last_modified_by = user.key other_post = mocks.create_post(user.key, institution.key) other_post.last_modified_by = user.key other_post.delete(user) post.put() other_post.put() posts = self.testapp.get("/api/institutions/%s/timeline?page=0&&limit=2" % institution.key.urlsafe()) post_json = (posts.json['posts'])[0] self.assertEqual( len(posts.json['posts']), 1, "Number of posts should be equal 1" ) self.assertEqual( post_json, post.make(posts.request.host), "The maked post should be equal to the expected one" )
def test_put_request_accepted(self, verify_token): """Test put request accepted.""" admin = mocks.create_user(ADMIN['email']) institution = mocks.create_institution() institution.admin = admin.key institution.photo_url = 'tst.jpg' institution.put() admin.institutions_admin = [institution.key] admin.add_permission("answer_user_request", institution.key.urlsafe()) admin.put() otheruser = mocks.create_user(USER['email']) data = { 'sender_key': otheruser.key.urlsafe(), 'is_request': True, 'admin_key': admin.key.urlsafe(), 'institution_key': institution.key.urlsafe(), 'office': 'Dev', 'type_of_invite': 'REQUEST_USER' } request = RequestUser.create(data) request.put() self.testapp.put( '/api/requests/' + request.key.urlsafe() + '/user', headers={'Institution-Authorization': institution.key.urlsafe()}) with self.assertRaises(Exception) as ex: self.testapp.put('/api/requests/' + request.key.urlsafe() + '/user') exception_message = self.get_message_exception(ex.exception.message) self.assertEqual( "Error! this request has already been processed", exception_message, "Expected error message is Error! this request has already been processed" )
def initModels(cls): """Init the tests' common models.""" # new User user cls.user = mocks.create_user('*****@*****.**') # new User second_user cls.second_user = mocks.create_user('*****@*****.**') # new Institution institution cls.institution = mocks.create_institution() cls.institution.members = [cls.user.key, cls.second_user.key] cls.institution.followers = [cls.user.key, cls.second_user.key] cls.institution.admin = cls.user.key cls.institution.put() # update user cls.user.institutions = [cls.institution.key] cls.user.follows = [cls.institution.key] cls.user.institutions_admin = [cls.institution.key] cls.user.add_permission("publish_post", cls.institution.key.urlsafe()) cls.user.add_permission("remove_member", cls.institution.key.urlsafe()) cls.user.put() cls.second_user.institutions = [cls.institution.key] cls.second_user.follows = [cls.institution.key] cls.second_user.add_permission("publish_post", cls.institution.key.urlsafe()) cls.second_user.put() # create headers cls.headers = {'Institution-Authorization': cls.institution.key.urlsafe()}
def test_post_user_not_allowed(self, verify_token, send_invite): """Test post user not allowed.""" # new Institution other_institution = mocks.create_institution() # new user second_user = mocks.create_user('*****@*****.**') # set the other_institution admin to be the second_user second_user.institutions_admin = [other_institution.key] second_user.put() other_institution.admin = second_user.key other_institution.put() with self.assertRaises(Exception) as ex: self.body['data']['admin_key'] = second_user.key.urlsafe() self.body['data'][ 'institution_key'] = other_institution.key.urlsafe() self.testapp.post_json("/api/invites/institution", self.body) message = self.get_message_exception(ex.exception.message) self.assertEqual( message, 'Error! User is not allowed to post invite', 'Expected exception message must be equal to Error! User is not allowed to post invite' ) # assert the invite was not sent to the invitee send_invite.assert_not_called()
def test_create_message(self): """Test create_message method.""" sender = mocks.create_user() sender.photo_url = "photo-url" sender.put() institution = mocks.create_institution() current_institution = institution.key message = service_messages.create_message(sender.key, current_institution) expected_message = { 'from': { 'name': sender.name.encode('utf8'), 'photo_url': sender.photo_url, 'institution_name': '' }, 'to': { 'institution_name': '' }, 'current_institution': { 'name': institution.name } } self.assertEquals( message, json.dumps(expected_message), "The created message should be igual to the expected one but was: "+message )
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_post_invite_user_not_admin(self, verify_token): """Test post invite user not admin.""" admin = mocks.create_user() other_user = mocks.create_user() institution = mocks.create_institution() institution.add_member(admin) institution.set_admin(admin.key) admin.put() institution.put() invite_body = create_body([other_user.email[0]], admin, institution, 'USER') verify_token._mock_return_value = {'email': admin.email[0]} with self.assertRaises(Exception) as raises_context: self.testapp.post_json('/api/invites/user', invite_body, headers={ 'institution-authorization': institution.key.urlsafe() }) message = self.get_message_exception(str(raises_context.exception)) self.assertEqual( message, 'Error! User is not allowed to send invites', "Expected error message is Error! User is not allowed to send invites" )
def setUp(cls): """Provide the base for the tests.""" super(InviteInstitutionHandlerTest, cls).setUp() app = cls.webapp2.WSGIApplication([ (InviteInstitutionHandlerTest.INVITE_INSTITUTION_URI, InviteInstitutionHandler), ], debug=True) cls.testapp = cls.webtest.TestApp(app) # create models # new User cls.user_admin = mocks.create_user('*****@*****.**') # Other user cls.other_user = mocks.create_user('*****@*****.**') # new Institution inst test cls.inst_test = mocks.create_institution() cls.inst_test.admin = cls.user_admin.key cls.inst_test.state = "active" cls.inst_test.put() # New invite user cls.data = { 'invitee': '*****@*****.**', 'admin_key': cls.user_admin.key.urlsafe(), 'institution_key': cls.inst_test.key.urlsafe(), 'suggestion_institution_name': 'test' } cls.invite = InviteInstitution.create(cls.data) cls.invite.put()
def test_post_with_wrong_institution(self, verify_token): """Test post with wrong institution.""" admin = mocks.create_user(ADMIN['email']) institution = mocks.create_institution() institution.add_member(admin) admin.add_institution(institution.key) admin.add_institution_admin(institution.key) admin.add_institution(institution.key) institution.set_admin(admin.key) admin.add_permission("send_link_inst_request", institution.key.urlsafe()) admin.put() institution.put() inst_requested = mocks.create_institution() admin_requested = mocks.create_user(USER['email']) inst_requested.add_member(admin_requested) admin.add_institution(inst_requested.key) admin.add_institution_admin(inst_requested.key) admin.add_institution(inst_requested.key) admin_requested.put() inst_requested.set_admin(admin_requested.key) inst_requested.put() data = { 'sender_key': admin.key.urlsafe(), 'is_request': True, 'admin_key': admin_requested.key.urlsafe(), 'institution_key': institution.key.urlsafe(), 'institution_requested_key': inst_requested.key.urlsafe(), 'type_of_invite': 'REQUEST_INSTITUTION_CHILDREN' } with self.assertRaises(Exception) as ex: self.testapp.post_json("/api/institutions/" + inst_requested.key.urlsafe() + "/requests/institution_children", data, headers={ 'institution-authorization': institution.key.urlsafe() }) exception_message = self.get_message_exception(ex.exception.message) self.assertEqual( "Error! User is not allowed to send request", exception_message, "Expected error message is Error! User is not allowed to send request" )
def test_post(self, verify_token): """Test method post of InstitutionParentRequestCollectionHandler.""" admin = mocks.create_user(ADMIN['email']) institution = mocks.create_institution() admin.institutions_admin = [institution.key] admin.add_institution(institution.key) institution.admin = admin.key admin.add_permission("send_link_inst_request", institution.key.urlsafe()) admin.put() institution.put() other_user = mocks.create_user(USER['email']) inst_requested = mocks.create_institution() admin_requested = mocks.create_user(ADMIN['email']) admin.institutions_admin = [inst_requested.key] admin.add_institution(inst_requested.key) admin_requested.put() inst_requested.admin = admin_requested.key inst_requested.put() data = { 'sender_key': admin.key.urlsafe(), 'is_request': True, 'admin_key': admin_requested.key.urlsafe(), 'institution_key': institution.key.urlsafe(), 'institution_requested_key': inst_requested.key.urlsafe(), 'type_of_invite': 'REQUEST_INSTITUTION_CHILDREN' } request = self.testapp.post_json( "/api/institutions/" + institution.key.urlsafe() + "/requests/institution_children", data, headers={'institution-authorization': institution.key.urlsafe()}) request = json.loads(request._app_iter[0]) institution = institution.key.get() self.assertEqual(request['sender'], admin.email, 'Expected sender email is [email protected]') self.assertEqual(request['admin_name'], admin_requested.name, 'Expected admin is admin_requested is User Admin') self.assertEqual( request['type_of_invite'], 'REQUEST_INSTITUTION_CHILDREN', 'Expected sender type_of_invite is REQUEST_INSTITUTION_CHILDREN')
def test_post(self): """Test the post method.""" # Verify the members admin = mocks.create_user() common_user = mocks.create_user() institution = mocks.create_institution() child_institution = mocks.create_institution() institution.address = mocks.create_address() admin.institutions_admin = [institution.key] admin.add_institution(institution.key) admin.follows = [institution.key] institution.members = [admin.key, common_user.key] institution.set_admin(admin.key) child_institution.parent_institution = institution.key child_institution.admin = common_user.key common_user.institutions_admin = [child_institution.key] institution.children_institutions.append(child_institution.key) institution.put() child_institution.put() common_user.add_institution(institution.key) common_user.follows = [institution.key] admin.put() common_user.put() self.assertTrue(institution.key in admin.institutions) self.assertTrue(institution.key in common_user.institutions) admin.add_permissions(DEFAULT_ADMIN_PERMISSIONS, institution.key.urlsafe()) admin.add_permissions(DEFAULT_ADMIN_PERMISSIONS, child_institution.key.urlsafe()) # Call the post method self.testapp.post( '/api/queue/remove-inst?institution_key=%s&remove_hierarchy=true&user_key=%s' % (institution.key.urlsafe(), admin.key.urlsafe())) # Retrieving the entities admin = admin.key.get() common_user = common_user.key.get() child_institution = child_institution.key.get() # Check if the method worked as expected self.assertFalse( has_permissions(admin, institution.key.urlsafe(), DEFAULT_ADMIN_PERMISSIONS)) self.assertFalse( has_permissions(admin, child_institution.key.urlsafe(), DEFAULT_ADMIN_PERMISSIONS)) self.assertTrue(child_institution.state == 'inactive')
def test_post_invite_instituion_parent(self, enqueue_task, verify_token): """Test post invite instituion parent.""" admin = mocks.create_user() other_user = mocks.create_user() institution = mocks.create_institution() institution.add_member(admin) institution.set_admin(admin.key) admin.add_permissions(DEFAULT_ADMIN_PERMISSIONS, institution.key.urlsafe()) admin.put() institution.put() invite_body = create_body(other_user.email[0], admin, institution, 'INSTITUTION_PARENT') verify_token._mock_return_value = {'email': admin.email[0]} response = self.testapp.post_json( '/api/invites/hierarchy', invite_body, headers={'institution-authorization': institution.key.urlsafe()}) invite = ndb.Key(urlsafe=response.json['invite']['key']).get() expected_make = { 'invitee': other_user.email[0], 'type_of_invite': "INSTITUTION_PARENT", 'admin_name': admin.name, 'sender_name': admin.name, 'key': invite.key.urlsafe(), 'status': 'sent', 'institution_admin': institution.make(['name']), 'institution': institution.make(Invite.INST_PROPS_TO_MAKE), 'suggestion_institution_name': 'new_inst', 'stub_institution': invite.stub_institution_key.get().make(['name', 'key', 'state']) } self.assertEquals(expected_make, invite.make()) enqueue_task.assert_called_with( 'send-invite', { 'invites_keys': json.dumps([invite.key.urlsafe()]), 'host': response.request.host, 'current_institution': institution.key.urlsafe() })