def test_add_user(self): with app.app_context(): mongo.db.credentials.remove({'email': self._email}) result = mongo.db.credentials.find_one({'email': self._email}) self.assertIsNone(result) add_fake_credentials(self._email) with app.app_context(): result = mongo.db.credentials.find_one({'email': self._email}) self.assertIsNotNone(result) credentials = oauthcredentials.get_token(self._email) self.assertIsNotNone(credentials)
def test_save_document(self): """Test that only the five most recent uploaded items per user are saved in the database. """ with app.app_context(): fs = gridfs.GridFS(mongo.db, 'document_fs') documents = fs.find( {'email': self._email } ) for document in documents: fs.delete(document._id) count = 0 documents = fs.find( {'email': self._email } ) for doc in documents: count += 1 self.assertEqual(count, 0) file_name = 'a.txt' path = os.path.join(self._path, 'docs', file_name) doc_ids = [] for i in range(7): with open(path, 'rb') as test_file: _id = save_document(file_handle=test_file, document_name=file_name, email=self._email) self.assertIsNotNone(_id) doc_ids.append(_id) count = 0 documents = fs.find( {'email': self._email } ) for doc in documents: count += 1 self.assertEqual(count, 3) for i in range(2): self.assertFalse(fs.exists(_id=ObjectId(doc_ids[i]), email=self._email))
def setUp(self): user = os.getenv('UNIFLOW_USER') self.assertNotEqual(None, user) self._email = '{}@students.calvin.edu'.format(user) self._path = os.path.abspath(os.path.dirname(__file__)) with app.app_context(): self._token = printapp.oauthcredentials.get_token(self._email) self.assertIsNotNone(self._token)
def add_fake_credentials(email, hasprinter=True): token = 'fakeoauth.py|noprinter' if hasprinter: token = 'fakeoauth.py' credentials = OAuth2Credentials(token, token, token, token, None, token, token) with app.app_context(): oauthcredentials._save_credentials(email, credentials)
def setUp(self): self._email = None user = os.getenv('UNIFLOW_USER') self.assertNotEqual(None, user) self._email = '{}@students.calvin.edu'.format(user) with app.app_context(): self._saved_credentials = oauth._get_credentials(self._email) oauth.delete_credentials(self._email, revoke=False)
def test_get_code_from_url(self): with app.app_context(): url = None self.assertRaises(ValueError, oauth.get_code_from_url, url) url = 'http://example.com' self.assertRaises(ValueError, oauth.get_code_from_url, url) url = 'http://example.com/?error=access_denied' self.assertRaises(ValueError, oauth.get_code_from_url, url) url = 'http://example.com/?code=HELLO' self.assertEqual(oauth.get_code_from_url(url), 'HELLO')
def setUp(self): self._email = '*****@*****.**' self._credentials = oauth2client.client.OAuth2Credentials( access_token='token', client_id='bar', client_secret='bar', refresh_token='refresh', token_expiry=datetime.datetime.now() + datetime.timedelta(days=30), token_uri='http://example.com', user_agent='foo') with app.app_context(): oauth._save_credentials(self._email, self._credentials)
def test_delete_document(self): with app.app_context(): file_name = 'test.docx' path = os.path.join(self._path, 'docs', file_name) with open(path, 'rb') as test_file: _id = save_document(file_handle=test_file, document_name=file_name, email=self._email) self.assertIsNotNone(_id) delete_document(_id, self._email) retrieved_file = get_document(document_id=_id, email=self._email) self.assertIsNone(retrieved_file)
def test_oauth_flow(self): with app.app_context(): app.config[ 'OAUTH_REDIRECT_URI'] = 'http://localhost:5000/oauthredirect' token = oauth.get_token(self._email) self.assertIsNone(token) url = oauth.get_authentication_prompt_url(self._email) self.assertIsNotNone(url) # open the oauth prompt url in a browser # start a web server and listen for the oauth callback s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) s.bind(('localhost', 5000)) s.listen(1) s.settimeout(20) webbrowser.open(url) conn, addr = s.accept() conn.settimeout(20) try: response = conn.recv(4096) except socket.timeout: self.fail('Timeout while waiting for oauth callback') conn.send('HTTP/1.1 200 OK') conn.close() s.close() data = response.split() self.assertTrue(len(data) > 1) self.assertEqual(data[0], 'GET') url = data[1] url = 'http://example.com' + url access_code = oauth.get_code_from_url(url) token = oauth.authorize_user_by_code(access_code, self._email) token_from_db = oauth.get_token(self._email) self.assertEqual(token, token_from_db) credentials = oauth._get_credentials(self._email) self.assertIsNotNone(credentials) self._saved_credentials = oauth._get_credentials(self._email) credentials.token_expiry = datetime.datetime.now( ) - datetime.timedelta(minutes=15) oauth._save_credentials(self._email, credentials) # Check if the access token is getting refreshed correctly. token = oauth.get_token(self._email) self.assertIsNotNone(token) self._saved_credentials = oauth._get_credentials(self._email)
def test_oauth_flow(self): with app.app_context(): app.config['OAUTH_REDIRECT_URI'] = 'http://localhost:5000/oauthredirect' token = oauth.get_token(self._email) self.assertIsNone(token) url = oauth.get_authentication_prompt_url(self._email) self.assertIsNotNone(url) # open the oauth prompt url in a browser # start a web server and listen for the oauth callback s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) s.bind(('localhost', 5000)) s.listen(1) s.settimeout(20) webbrowser.open(url) conn, addr = s.accept() conn.settimeout(20) try: response = conn.recv(4096) except socket.timeout: self.fail('Timeout while waiting for oauth callback') conn.send('HTTP/1.1 200 OK') conn.close() s.close() data = response.split() self.assertTrue(len(data) > 1) self.assertEqual(data[0], 'GET') url = data[1] url = 'http://example.com' + url access_code = oauth.get_code_from_url(url) token = oauth.authorize_user_by_code(access_code, self._email) token_from_db = oauth.get_token(self._email) self.assertEqual(token, token_from_db) credentials = oauth._get_credentials(self._email) self.assertIsNotNone(credentials) self._saved_credentials = oauth._get_credentials(self._email) credentials.token_expiry = datetime.datetime.now() - datetime.timedelta(minutes=15) oauth._save_credentials(self._email, credentials) # Check if the access token is getting refreshed correctly. token = oauth.get_token(self._email) self.assertIsNotNone(token) self._saved_credentials = oauth._get_credentials(self._email)
def test_submit_job(self): """Test submitting jobs to uniflow. This test is disabled by default. Uncomment the decorator above to enable it. """ with app.app_context(): file_names = ['test.pdf', 'test.txt', 'test.doc', 'test.docx'] for name in file_names: path = os.path.join(self._path, 'docs', name) test_file = open(path, 'rb') printapp.cloudprint.submit_job(self._token, test_file, color=False, duplex=True, copies=1, collate=False) test_file.close()
def test_make_ticket(self): with app.app_context(): ticket1 = printapp.cloudprint._make_print_ticket() self.assertIsNotNone(ticket1) ticket2 = printapp.cloudprint._make_print_ticket(False, False, 1, True) self.assertIsNotNone(ticket2) self.assertEqual(ticket1, ticket2) ticket3 = printapp.cloudprint._make_print_ticket(True, True, 12, True) self.assertIsNotNone(ticket3) ticket4 = printapp.cloudprint._make_print_ticket(color=True, duplex=True, copies=12, collate=False) self.assertIsNotNone(ticket4) self.assertNotEqual(ticket3, ticket4) ticket5 = printapp.cloudprint._make_print_ticket(collate=True) self.assertIsNotNone(ticket5) self.assertEqual(ticket1, ticket5)
def test_get_and_save_document(self): with app.app_context(): file_name = 'test.docx' path = os.path.join(self._path, 'docs', file_name) with open(path, 'rb') as test_file: _id = save_document(file_handle=test_file, document_name=file_name, email=self._email) self.assertIsNotNone(_id) retrieved_file = get_document(document_id=_id, email=self._email) retrieved_data = retrieved_file.read() with open(path, 'rb') as test_file: expected_data = test_file.read() self.assertEqual(len(expected_data), len(retrieved_data)) self.assertTrue(expected_data == retrieved_data)
def test_make_ticket(self): with app.app_context(): ticket1 = printapp.cloudprint._make_print_ticket() self.assertIsNotNone(ticket1) ticket2 = printapp.cloudprint._make_print_ticket( False, False, 1, True) self.assertIsNotNone(ticket2) self.assertEqual(ticket1, ticket2) ticket3 = printapp.cloudprint._make_print_ticket( True, True, 12, True) self.assertIsNotNone(ticket3) ticket4 = printapp.cloudprint._make_print_ticket(color=True, duplex=True, copies=12, collate=False) self.assertIsNotNone(ticket4) self.assertNotEqual(ticket3, ticket4) ticket5 = printapp.cloudprint._make_print_ticket(collate=True) self.assertIsNotNone(ticket5) self.assertEqual(ticket1, ticket5)
def test_save_document(self): """Test that only the five most recent uploaded items per user are saved in the database. """ with app.app_context(): fs = gridfs.GridFS(mongo.db, 'document_fs') documents = fs.find({'email': self._email}) for document in documents: fs.delete(document._id) count = 0 documents = fs.find({'email': self._email}) for doc in documents: count += 1 self.assertEqual(count, 0) file_name = 'a.txt' path = os.path.join(self._path, 'docs', file_name) doc_ids = [] for i in range(7): with open(path, 'rb') as test_file: _id = save_document(file_handle=test_file, document_name=file_name, email=self._email) self.assertIsNotNone(_id) doc_ids.append(_id) count = 0 documents = fs.find({'email': self._email}) for doc in documents: count += 1 self.assertEqual(count, 3) for i in range(2): self.assertFalse( fs.exists(_id=ObjectId(doc_ids[i]), email=self._email))
def tearDown(self): with app.app_context(): oauth._save_credentials(self._email, self._saved_credentials)
def test_get_token(self): with app.app_context(): token = oauth.get_token(self._email) self.assertIsNotNone(token)
def test_delete_token(self): with app.app_context(): oauth.delete_credentials(self._email, revoke=False) self.assertIsNone(oauth.get_token(self._email))
def remove_credentials(email): with app.app_context(): mongo.db.credentials.remove({'email': email})
def test_has_uniflow_printer(self): with app.app_context(): result = printapp.cloudprint.has_uniflow_printer(token=self._token) self.assertEqual(result, True)