class TestAPIResources(unittest.TestCase):

    def setUp(self):
        self.event = Event()

    def run_tests(self, id):
        self.resource = resources[id]
        self.num_methods = num_valid_methods[id]
        self.event.setPath(self.resource)
        # event = Event()

        # test all valid methods
        for method in methods[:self.num_methods]:
            self.event.setHttpMethod(method)
            msg = message_template.format(self.resource, method.lower())
            res = main_handler(self.event.getEvent(), None)
            self.assertEqual(res['statusCode'], 200)
            body = ast.literal_eval(res['body'])
            self.assertEqual(body['message'], msg)

        # test all invalid methods
        for method in methods[self.num_methods:]:
            self.event.setHttpMethod(method)
            res = main_handler(self.event.getEvent(), None)
            self.assertEqual(res['statusCode'], 501)
            self.assertEqual(res['body'], str(None))
示例#2
0
class TestUsers(unittest.TestCase):
    @classmethod
    def setUpClass(cls):
        global connection
        # connect to database
        try:
            connection = pymysql.connect(rds_host,
                                         user=name,
                                         passwd=password,
                                         db=db_name,
                                         connect_timeout=5)
        except pymysql.err.Error as ex:
            template = "ERROR: {0} - Could not connect to MySql instance \n{1!r}"
            message = template.format(type(ex).__name__, ex.args)
            print(message)
            sys.exit()

    @classmethod
    def tearDownClass(cls):
        connection.close()

    def setUp(self):
        self.event = Event()
        self.event.setPath(path)
        self.req_body = dict()

    def tearDown(self):
        # clean up the database
        with connection.cursor() as cur:
            cur.execute('SELECT * FROM user WHERE email="*****@*****.**"')
            li = cur.fetchall()
            for row in li:
                cur.execute('DELETE FROM user WHERE user_id=' + str(row[0]))
            connection.commit()

    def test_add_new_user(self):
        self.event.setHttpMethod('POST')

        # add user with required fields
        self.req_body['display_name'] = 'test_user'
        self.req_body['email'] = '*****@*****.**'
        self.req_body['first_name'] = 'Test'
        self.req_body['last_name'] = 'Case'
        self.event.setBody(json.dumps(self.req_body))

        response = main_handler(self.event.getEvent(), context)

        resp_body = json.loads(response['body'])
        status_code = response['statusCode']

        # check for correct status code
        self.assertEqual(201, status_code)

        # check for correct body
        self.assertEqual(None, resp_body)
示例#3
0
 def setUp(self):
     self.event = Event()
     self.event.setPath(path)
     self.req_body = dict()
示例#4
0
class TestEvents(unittest.TestCase):
    @classmethod
    def setUpClass(cls):
        global connection
        # connect to database
        try:
            connection = pymysql.connect(rds_host,
                                         user=name,
                                         passwd=password,
                                         db=db_name,
                                         connect_timeout=5)
        except pymysql.err.Error as ex:
            template = "ERROR: {0} - Could not connect to MySql instance \n{1!r}"
            message = template.format(type(ex).__name__, ex.args)
            print(message)
            sys.exit()

    @classmethod
    def tearDownClass(cls):
        connection.close()

    def setUp(self):
        self.event = Event()
        self.event.setPath(path)
        self.req_body = dict()

    def tearDown(self):
        # clean up the database
        with connection.cursor() as cur:
            cur.execute('SELECT * FROM event WHERE name="Best Event"')
            li = cur.fetchall()
            for row in li:
                cur.execute('DELETE FROM event WHERE event_id=' + str(row[0]))
            connection.commit()

            cur.execute(
                'SELECT * FROM location WHERE latitude=0.000 AND longitude=0.000'
            )
            li = cur.fetchall()
            for row in li:
                cur.execute('DELETE FROM location WHERE location_id=' +
                            str(row[0]))
            connection.commit()

    def test_add_new_event(self):
        self.event.setHttpMethod('POST')
        test_dict = {'events': []}
        event_name = 'Best Event'
        event_type = 'Music'
        is_public = True
        event_date = '2018-06-15'
        start_time = '15:30:00'
        end_time = '20:30:00'
        points = 9000
        is_free = False
        location = {
            "street": 'Lame St.',
            "state": 'CA',
            "city": 'San Diego',
            "country": 'United States',
            "zip": 92364,
            "latitude": 0.000,
            "longitude": 0.000
        }

        test_dict['events'].append({
            'name': event_name,
            'start_time': start_time,
            'end_time': end_time,
            'is_free': is_free,
            'location': location,
            'points': points,
            'is_public': is_public,
            'event_date': event_date,
            'type': event_type
        })
        self.req_body['events'] = test_dict['events']
        self.event.setBody(json.dumps(self.req_body))
        response = main_handler(self.event.getEvent(), context)
        resp_body = json.loads(response['body'])
        print(resp_body)
        status_code = response['statusCode']

        # check for correct status code
        self.assertEqual(201, status_code)

        # check for correct body
        self.assertEqual(None, resp_body)

    def test_add_new_event_frontend(self):
        self.event.setHttpMethod('POST')
        event_name = 'Best Event'
        event_type = 'Music'
        is_public = True
        event_date = '2018-06-15'
        start_time = '15:30:00'
        end_time = '20:30:00'
        points = 9000
        is_free = False
        location = {
            "street": 'Lame St.',
            "state": 'CA',
            "city": 'San Diego',
            "country": 'United States',
            "zip": 92364,
            "latitude": 0.000,
            "longitude": 0.000
        }

        self.req_body = {
            'name': event_name,
            'start_time': start_time,
            'end_time': end_time,
            'is_free': is_free,
            'location': location,
            'points': points,
            'is_public': is_public,
            'event_date': event_date,
            'type': event_type
        }

        self.event.setBody(json.dumps(self.req_body))
        response = main_handler(self.event.getEvent(), context)
        resp_body = json.loads(response['body'])
        status_code = response['statusCode']

        # check for correct status code
        self.assertEqual(201, status_code)

        # check for correct body
        self.assertEqual(None, resp_body)

    def test_get_event_by_id(self):
        self.event.setHttpMethod('GET')
        self.event.setQueryStringParameters({'event_id': "27"})
        response = main_handler(self.event.getEvent(), context)
        status_code = response['statusCode']

        # check for correct status code
        self.assertEqual(200, status_code)
示例#5
0
class TestEvents(unittest.TestCase):
    @classmethod
    def setUpClass(cls):
        global connection
        # connect to database
        try:
            connection = pymysql.connect(rds_host,
                                         user=name,
                                         passwd=password,
                                         db=db_name,
                                         connect_timeout=5)
        except pymysql.err.Error as ex:
            template = "ERROR: {0} - Could not connect to MySql instance \n{1!r}"
            message = template.format(type(ex).__name__, ex.args)
            print(message)
            sys.exit()

    @classmethod
    def tearDownClass(cls):
        connection.close()

    def setUp(self):
        self.event = Event()
        self.event.setPath(path)
        self.req_body = dict()

    def tearDown(self):
        # clean up the database
        with connection.cursor() as cur:
            cur.execute('SELECT * FROM mission WHERE name="New Mission"')
            li = cur.fetchall()
            for row in li:
                cur.execute('DELETE FROM mission WHERE mission_id=' +
                            str(row[0]))
            connection.commit()

            cur.execute(
                'SELECT * FROM location WHERE latitude=0.000 AND longitude=0.000'
            )
            li = cur.fetchall()
            for row in li:
                cur.execute('DELETE FROM location WHERE location_id=' +
                            str(row[0]))
            connection.commit()

    def test_add_new_event(self):
        self.event.setHttpMethod('POST')
        test_dict = {'missions': []}
        mission_name = 'New Mission'
        mission_type = 'Adventure'
        mission_date = '2018-06-15'
        start_time = '15:30:00'
        end_time = '20:30:00'
        description = "Sucks to succk"
        image = ""
        points = 9000
        location = {
            "street": 'Lame St.',
            "state": 'CA',
            "city": 'san diego',
            "country": 'United States',
            "zip": 92364,
            "latitude": 0.000,
            "longitude": 0.000
        }

        test_dict['missions'].append({
            'name': mission_name,
            'mission_start': start_time,
            'mission_end': end_time,
            'description': description,
            'location': location,
            'points': points,
            'mission_date': mission_date,
            'image': image
        })
        self.req_body['missions'] = test_dict['missions']
        self.event.setBody(json.dumps(self.req_body))
        response = main_handler(self.event.getEvent(), context)
        resp_body = json.loads(response['body'])
        print(resp_body)
        status_code = response['statusCode']
        print(response)

        # check for correct status code
        self.assertEqual(201, status_code)

        # check for correct body
        self.assertEqual(None, resp_body)
 def setUp(self):
     self.event = Event()