def test_reject_booking(self): tester = app.test_client() # register a user and get token register = tester.post('api/user/new', data=dict(username='******', email='*****@*****.**', password='******')) token = json.loads(register.data)['token'] # add an item to db tester.post(f'api/item/new?token={token}', data=dict(name='new item', description='test description', category='cat', deposit=1.00, overdue_charge=1.00)) return_date = 5 # request a booking tester.post(f'api/booking/new?token={token}', data=dict(item_id=1, return_by=return_date)) # reject a booking tester.delete(f'api/booking/1?token={token}', content_type="html/text") # get all booking requests response = tester.get(f'api/booking/requests?token={token}', content_type='html/text') self.assertEqual(response.data, b'[]\n')
def test_1_populated(self): newUser = User(username='******', email='*****@*****.**', password_hash='test', created_at=datetime(2019, 11, 1), postcode='e49qr', latitude=51.7655451, longitude=-1.257095) db.session.add(newUser) newItem = Item( name='test', description="testing", category='test', owner_id=1, deposit=1.00, overdue_charge=1.00, created_at=datetime(2019, 11, 1), longitude=-1.257095, latitude=51.7655451, ) expected_output = b'{"category":"test","created_at":"01/11/2019","deposit":"1.0","description":"testing","id":1,"latitude":51.7655451,"longitude":-1.257095,"name":"test","overdue_charge":"1.0","owner_id":1}\n' db.session.add(newItem) db.session.commit() tester = app.test_client(self) response = tester.get('api/item/1', content_type='html/text') print(response.data) # print(blah) self.assertEqual(response.status_code, 200) self.assertEqual(response.data, expected_output)
def test_my_items_displayed(self): tester = app.test_client() # register a user and get token register = tester.post('api/user/new', data=dict(username='******', email='*****@*****.**', password='******')) token = json.loads(register.data)['token'] # add an item to db tester.post(f'api/item/new?token={token}', data=dict(name='new item', description='test description', category='cat', deposit=1.00, overdue_charge=1.00)) # access our items response = tester.get(f'api/items?token={token}', content_type='html/text') self.assertIn(b'new item', response.data) # register a new user and get new token register = tester.post('api/user/new', data=dict(username='******', email='*****@*****.**', password='******')) token = json.loads(register.data)['token'] # access our items, as different user (expect none) response = tester.get(f'api/items?token={token}', content_type='html/text') self.assertNotIn(b'new item', response.data)
def test_item_not_found(self): tester = app.test_client(self) response = tester.get('api/item/4', content_type='html/text') self.assertEqual(response.status_code, 404) self.assertEqual( json.loads(response.data), {"error": "Item not found"}, )
def test_user_added_to_database(self): tester = app.test_client(self) response = tester.post('api/user/new', data=dict(username='******', email='*****@*****.**', password='******')) self.assertEqual(response.status_code, 200) self.assertIn('successfully added user: new user', json.loads(response.data)['message'])
def test_user_does_not_exist(self): tester = app.test_client(self) response = tester.post('login', data=dict(username='******', password="******")) self.assertEqual(response.status_code, 400) self.assertEqual( json.loads(response.data), {"error": "User does not exist"}, )
def test_token_created(self): tester = app.test_client(self) register = tester.post('api/user/new', data=dict(username='******', email='*****@*****.**', password='******')) token = json.loads(register.data)['token'] self.assertTrue( jwt.decode(token, app.config.get('SECRET_KEY'), algorithms='HS256'))
def test_missing_params_raises_error(self): tester = app.test_client(self) self.assertRaises( Exception, tester.post('api/item/new', data=dict(name='new item', category='cat', email='*****@*****.**', deposit=1.00, overdue_charge=1.00)))
def test_fail_if_token_expired(self): tester = app.test_client(self) token = 'eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJleHAiOjE1NzI3MTgzMDEsImlhdCI6MTU3MjcxODI3MSwic3ViIjoxOH0.AIBoO1qbh0KHDKnAs56NP2BVH8NoO2jFgNmN7RUKxYw' self.assertRaises( Exception, tester.post(f'api/item/new?token={token}', data=dict(name='new item', description='test description', category='cat', email='*****@*****.**', deposit=1.00, overdue_charge=1.00)))
def test_insufficient_info(self): tester = app.test_client(self) tester.post('api/user/new', data=dict(username='******', email='*****@*****.**', password='******')) response = tester.post('login', data=dict(username='******')) self.assertEqual(response.status_code, 400) self.assertEqual( json.loads(response.data), {"error": "Insufficient information"}, )
def test_error_if_user_already_exists(self): tester = app.test_client(self) response = tester.post('api/user/new', data=dict(username='******', email='*****@*****.**', password='******')) self.assertEqual(response.status_code, 200) self.assertRaises( Exception, tester.post('api/user/new', data=dict(username='******', email='*****@*****.**', password='******')))
def test_successful(self): tester = app.test_client(self) tester.post('api/user/new', data=dict(username='******', email='*****@*****.**', password='******')) response = tester.post('login', data=dict(username='******', password='******')) response_message = json.loads(response.data)['message'] self.assertEqual(response.status_code, 200) self.assertEqual( response_message, 'successfully logged in user: new user', )
def test_user_1_populated(self): newUser = User(username='******', email='*****@*****.**', password_hash='test', created_at=datetime(2019, 11, 1), postcode='e49qr', latitude=51.7655451, longitude=-1.257095) expected_output = b'{"created_at":"01/11/2019","email":"*****@*****.**","id":1,"latitude":51.7655451,"longitude":-1.257095,"postcode":"e49qr","username":"******"}\n' db.session.add(newUser) db.session.commit() tester = app.test_client(self) response = tester.get('api/user/1', content_type='html/text') self.assertEqual(response.status_code, 200) self.assertEqual(response.data, expected_output)
def test_token_required(self): tester = app.test_client(self) register = tester.post('api/user/new', data=dict(username='******', email='*****@*****.**', password='******')) response = tester.post('api/item/new', data=dict(name='new item', description='test description', category='cat', email='*****@*****.**', deposit=1.00, overdue_charge=1.00)) forbidden_error_code = 403 self.assertEqual(response.status_code, forbidden_error_code)
def test_get_booking_requests(self): tester = app.test_client() register = tester.post('api/user/new', data=dict(username='******', email='*****@*****.**', password='******')) token = json.loads(register.data)['token'] tester.post(f'api/item/new?token={token}', data=dict(name='new item', description='test description', category='cat', deposit=1.00, overdue_charge=1.00)) return_in = 5 tester.post(f'api/booking/new?token={token}', data=dict(item_id=1, return_by=return_in)) response = tester.get(f'api/booking/requests?token={token}', content_type='html/text') self.assertIn(b'new item', response.data)
def test_item_added_to_database(self): tester = app.test_client(self) login = tester.post('api/user/new', data=dict(username='******', email='*****@*****.**', password='******', postcode='ox26sq')) token = json.loads(login.data)['token'] response = tester.post(f'api/item/new?token={token}', data=dict(name='new item', description='test description', category='cat', deposit=1.00, overdue_charge=1.00)) self.assertEqual(response.status_code, 200) self.assertEqual( json.loads(response.data), {'message': 'successfully added item: new item'}, )
def test_request_booking(self): tester = app.test_client(self) register = tester.post('api/user/new', data=dict(username='******', email='*****@*****.**', password='******')) token = json.loads(register.data)['token'] tester.post(f'api/item/new?token={token}', data=dict(name='new item', description='test description', category='cat', deposit=1.00, overdue_charge=1.00)) return_in = 7 return_by = (datetime.utcnow() + timedelta(days=int(return_in))).strftime("%d/%m/%Y") bytes_return_date = (str(return_by).encode()) response = tester.post(f'api/booking/new?token={token}', data=dict(item_id=1, return_by=return_in)) self.assertIn(bytes_return_date, response.data)
def test_getting_items_by_category(self): tester = app.test_client(self) register = tester.post('api/user/new', data=dict(username='******', email='*****@*****.**', password='******', postcode="e49qr")) token = json.loads(register.data)['token'] tester.post(f'api/item/new?token={token}', data=dict(name='new item', description='test description', category='clothes', deposit=1.00, overdue_charge=1.00)) tester.post(f'api/item/new?token={token}', data=dict(name='other item', description='test item', category='clothes', deposit=1.00, overdue_charge=1.00)) response2 = tester.get("/api/categories/clothes", content_type="html/text") self.assertIn(b'new item', response2.data)
def test_get_my_bookings(self): tester = app.test_client() # register a user and get token register = tester.post('api/user/new', data=dict(username='******', email='*****@*****.**', password='******')) token = json.loads(register.data)['token'] # add an item to db tester.post(f'api/item/new?token={token}', data=dict(name='new item', description='test description', category='cat', deposit=1.00, overdue_charge=1.00)) return_date = 5 #request a booking tester.post(f'api/booking/new?token={token}', data=dict(item_id=1, return_by=return_date)) # confirm booking tester.patch(f'api/booking/1?token={token}', data=dict(confirmed=True)) response = tester.get(f'api/bookings?token={token}', content_type='html/text') self.assertIn(b'new item', response.data)
def test_redirect(self): tester = app.test_client(self) response = tester.get('/', content_type='html/text') self.assertEqual(response.status_code, 302)
def test_user_1(self): tester = app.test_client(self) response = tester.get('/api/user/1', content_type='html/text') self.assertEqual(response.status_code, 404)
def test_item_index(self): tester = app.test_client(self) response = tester.get('/api/item/index', content_type='html/text') self.assertEqual(response.status_code, 200)
def test_missing_email_raises_error(self): tester = app.test_client(self) self.assertRaises( Exception, tester.post('api/user/new', data=dict(username='******', password="******")))