Ejemplo n.º 1
0
    def test_create_invalid_request(self):
        """Test create invalid request."""
        admin_user = mocks.create_user('*****@*****.**')
        other_user = mocks.create_user('*****@*****.**')
        inst_test = mocks.create_institution()
        admin_user.institutions_admin = [inst_test.key]
        inst_test.admin = admin_user.key
        admin_user.put()
        inst_test.put()
        data = {
            'sender_key': other_user.key.urlsafe(),
            'is_request': True,
            'admin_key': admin_user.key.urlsafe(),
            'institution_key': inst_test.key.urlsafe()
        }

        request = RequestUser.create(data)
        request.put()

        with self.assertRaises(FieldException) as ex:
            data = {
                'sender_key': other_user.key.urlsafe(),
                'is_request': True,
                'admin_key': admin_user.key.urlsafe(),
                'institution_key': inst_test.key.urlsafe()
            }

            RequestUser.create(data)

        self.assertEqual('The sender is already invited', str(ex.exception),
                         'Expected message is The sender is already invited')
Ejemplo n.º 2
0
    def test_create_request(self):
        """Test create new request."""
        admin_user = mocks.create_user('*****@*****.**')
        other_user = mocks.create_user('*****@*****.**')
        inst_test = mocks.create_institution()
        admin_user.institutions_admin = [inst_test.key]
        inst_test.admin = admin_user.key
        admin_user.put()
        inst_test.put()
        data = {
            'sender_key': other_user.key.urlsafe(),
            'is_request': True,
            'admin_key': admin_user.key.urlsafe(),
            'institution_key': inst_test.key.urlsafe()
        }

        request = RequestUser.create(data)
        request.put()

        self.assertEqual(request.sender_key, other_user.key,
                         'The sender of request expected was other user')

        self.assertTrue(request.is_request,
                        "The atribute is_request must be equal True")

        self.assertEqual(request.admin_key, admin_user.key,
                         'The admin of institution expected was Admin User')

        self.assertEqual(request.institution_key, inst_test.key,
                         'The key of institution expected was inst test')
Ejemplo n.º 3
0
    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"
        )
Ejemplo n.º 4
0
    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"
        )
Ejemplo n.º 5
0
def get_requests(user_key):
    """Query to return the list of requests that this user sent to institutions."""
    institutions_requested = []
    queryRequests = RequestUser.query(RequestUser.sender_key == user_key,
                                      RequestUser.status == 'sent')
    institutions_requested = [
        request.institution_key.urlsafe() for request in queryRequests
    ]

    return institutions_requested
Ejemplo n.º 6
0
    def test_make(self):
        """Test method make."""
        admin_user = mocks.create_user('*****@*****.**')
        other_user = mocks.create_user('*****@*****.**')
        inst_test = mocks.create_institution()
        admin_user.institutions_admin = [inst_test.key]
        inst_test.admin = admin_user.key
        admin_user.put()
        inst_test.put()
        data = {
            'sender_key': other_user.key.urlsafe(),
            'is_request': True,
            'admin_key': admin_user.key.urlsafe(),
            'institution_key': inst_test.key.urlsafe(),
            'office': 'teacher',
            'sender_name': 'other_user',
            'institutional_email': 'otheruser@inst_test.com'
        }

        request = RequestUser.create(data)
        request.put()

        make = {
            'status': 'sent',
            'institution_admin': {
                'name': inst_test.name
            },
            'sender': other_user.email,
            'admin_name': admin_user.name,
            'key': request.key.urlsafe(),
            'institution': inst_test.make(RequestUser.INST_PROPS_TO_MAKE),
            'type_of_invite': 'REQUEST_USER',
            'institution_key': inst_test.key.urlsafe(),
            'office': 'teacher',
            'sender_name': 'other_user',
            'sender_key': other_user.key.urlsafe(),
            'institutional_email': 'otheruser@inst_test.com'
        }

        request_make = request.make()
        for key in make.keys():
            if key == "institution":
                make_institution = make[key]
                request_institution = request_make[key]
                for inst_key in make_institution.keys():

                    self.assertEqual(
                        str(make_institution[inst_key]).encode('utf-8'),
                        str(request_institution[inst_key]).encode('utf-8'),
                        "The make object must be equal to variable make")
            else:
                self.assertEqual(
                    str(make[key]).encode('utf-8'),
                    str(request_make[key]).encode('utf-8'),
                    "The make object must be equal to variable make")
Ejemplo n.º 7
0
    def get(self, user, institution_key):
        """Get all requests which type is user and
        the institution_key is equals to the institution_key
        received as param. Only the sent requests.
        """
        queryRequests = RequestUser.query(
            RequestUser.institution_key == ndb.Key(urlsafe=institution_key),
            RequestUser.status == 'sent')

        requests = [request.make() for request in queryRequests]

        self.response.write(json.dumps(requests))
Ejemplo n.º 8
0
    def test_put(self, verify_token):
        """Test method put of RequestHandler."""
        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()})

        user = otheruser.key.get()
        institution = institution.key.get()

        self.assertEqual(request.key.get().status, 'accepted',
                         "expected status must be equal to accepted")

        self.assertTrue(institution.key in user.institutions,
                        "key of institution must be in user institutions")

        self.assertTrue(institution.key in user.follows,
                        "key of institution must be in user follows")

        self.assertTrue(user.key in institution.members,
                        "key of user must be in institution members")

        self.assertTrue(user.key in institution.followers,
                        "key of user must be in institution followers")
Ejemplo n.º 9
0
    def test_get(self, verify_token):
        """Test method get of RequestHandler."""
        admin = mocks.create_user(ADMIN['email'])
        institution = mocks.create_institution()
        admin.institutions_admin = [institution.key]
        institution.admin = admin.key
        admin.put()
        institution.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()

        response = self.testapp.get('/api/requests/' + request.key.urlsafe() +
                                    '/user')
        request_response = json.loads(response._app_iter[0])

        self.assertEqual(
            request_response['sender'],
            request.sender_key.get().email,
            "expected sender email must be equal to sender email of original request"
        )

        self.assertEqual(
            request_response['admin_name'],
            request.admin_key.get().name,
            "expected admin_name must be equal to admin_name of original request"
        )

        self.assertEqual(
            request_response['institution_key'],
            request.institution_key.urlsafe(),
            "expected institution_key must be equal to institution_key of original request"
        )

        self.assertEqual(
            request_response['type_of_invite'], 'REQUEST_USER',
            "expected type_of_invite must be equal to  REQUEST_USER")
Ejemplo n.º 10
0
    def test_delete(self, verify_token):
        """Test method delete of RequestHandler."""
        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'])
        profile_data = {
            'office': "member",
            'institution_name': institution.name,
            'institution_photo_url': "photo-url.com",
            'institution_key': institution.key.urlsafe()
        }
        otheruser.create_and_add_profile(profile_data)
        otheruser.put()
        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()
        self.assertEquals(len(otheruser.institution_profiles), 1,
                          'The other_user should have only one profile')

        self.testapp.delete(
            '/api/requests/' + request.key.urlsafe() + '/user',
            headers={'Institution-Authorization': institution.key.urlsafe()})

        # update request and other_user
        self.request = request.key.get()
        self.other_user = otheruser.key.get()

        self.assertEqual(self.request.status, 'rejected')
        self.assertEquals(len(self.other_user.institution_profiles), 0,
                          'The other_user should have no profile')