def test__getCUPSQueueNameAndPrinter(): assert helperinstance._getCUPSQueueNameAndPrinter('test') == (None, None) requestor = CloudPrintRequestor() requestor.setAccount("test") printerinstance = Printer( { 'name': 'Testing Printer', 'id': '__test_printer', 'capabilities': [{ 'name': 'ns1:Colors', 'type': 'Feature' }] }, requestor, helperinstance) helperinstance.addPrinter(printerinstance, "test") assert helperinstance._getCUPSQueueNameAndPrinter('gcp://test/__test_printer') == \ ("test", {'device-uri': 'gcp://test/__test_printer', 'printer-info': 'test', 'printer-is-shared': False, 'printer-location': 'Google Cloud Print', 'printer-state': 3, 'printer-state-message': '', 'printer-state-reasons': ['none'], 'printer-type': 1, 'printer-uri-supported': 'ipp://localhost/printers/test'})
def test__getCUPSQueueNameAndPrinter(): assert helperinstance._getCUPSQueueNameAndPrinter("test") == (None, None) requestor = CloudPrintRequestor() requestor.setAccount("test") printerinstance = Printer( { "name": "Testing Printer", "id": "__test_printer", "capabilities": [{"name": "ns1:Colors", "type": "Feature"}], }, requestor, helperinstance, ) helperinstance.addPrinter(printerinstance, "test") assert helperinstance._getCUPSQueueNameAndPrinter("gcp://test/__test_printer") == ( "test", { "device-uri": "gcp://test/__test_printer", "printer-info": "test", "printer-is-shared": False, "printer-location": "Google Cloud Print", "printer-state": 3, "printer-state-message": "", "printer-state-reasons": ["none"], "printer-type": 1, "printer-uri-supported": "ipp://localhost/printers/test", }, )
def test_getPrinters(): global mockcups assert isinstance(helperinstance.getPrinters(), dict) assert len(helperinstance.getPrinters()) == 0 # add printer requestor = CloudPrintRequestor() requestor.setAccount("test") printerinstance = Printer( { "name": "Testing Printer", "id": "__test_printer", "capabilities": [{"name": "ns1:Colors", "type": "Feature"}], }, requestor, helperinstance, ) helperinstance.addPrinter(printerinstance, "test") # also add dummy instance with non-gcp uri mockcups._printers["test-dummy-non-gcp-printer"] = { "printer-is-shared": False, "printer-info": "test info", "printer-state-message": "", "printer-type": 1, "printer-state-reasons": ["none"], "printer-uri-supported": "ipp://localhost/printers/test", "printer-state": 3, "printer-location": "test location", "device-uri": "test://test/test", } assert len(mockcups._printers) == 2 # test count of printers returned has increased by 1 assert len(helperinstance.getPrinters()) == 1 for printername in helperinstance.getPrinters(): assert printername == "test" printer = helperinstance.getPrinters()[printername] assert printer["printer-is-shared"] is False assert printer["device-uri"] == "gcp://test/__test_printer" # delete printer helperinstance.deletePrinter(printerinstance.getURI()) # test count of printers returned is same as original assert len(helperinstance.getPrinters()) == 0
def test_getPrinters(): global mockcups assert isinstance(helperinstance.getPrinters(), dict) assert len(helperinstance.getPrinters()) == 0 # add printer requestor = CloudPrintRequestor() requestor.setAccount("test") printerinstance = Printer( { 'name': 'Testing Printer', 'id': '__test_printer', 'capabilities': [{ 'name': 'ns1:Colors', 'type': 'Feature' }] }, requestor, helperinstance) helperinstance.addPrinter(printerinstance, "test") # also add dummy instance with non-gcp uri mockcups._printers["test-dummy-non-gcp-printer"] = { 'printer-is-shared': False, 'printer-info': "test info", 'printer-state-message': '', 'printer-type': 1, 'printer-state-reasons': ['none'], 'printer-uri-supported': 'ipp://localhost/printers/test', 'printer-state': 3, 'printer-location': "test location", 'device-uri': "test://test/test" } assert len(mockcups._printers) == 2 # test count of printers returned has increased by 1 assert len(helperinstance.getPrinters()) == 1 for printername in helperinstance.getPrinters(): assert printername == 'test' printer = helperinstance.getPrinters()[printername] assert printer['printer-is-shared'] is False assert printer['device-uri'] == 'gcp://test/__test_printer' # delete printer helperinstance.deletePrinter(printerinstance.getURI()) # test count of printers returned is same as original assert len(helperinstance.getPrinters()) == 0
def test__getCUPSQueueName(): assert helperinstance._getCUPSQueueName("test") is None requestor = CloudPrintRequestor() requestor.setAccount("test") printerinstance = Printer( { "name": "Testing Printer", "id": "__test_printer", "capabilities": [{"name": "ns1:Colors", "type": "Feature"}], }, requestor, helperinstance, ) helperinstance.addPrinter(printerinstance, "test") assert helperinstance._getCUPSQueueName("gcp://test/__test_printer") == "test"
def test__getCUPSQueueName(): assert helperinstance._getCUPSQueueName('test') is None requestor = CloudPrintRequestor() requestor.setAccount("test") printerinstance = Printer( { 'name': 'Testing Printer', 'id': '__test_printer', 'capabilities': [{ 'name': 'ns1:Colors', 'type': 'Feature' }] }, requestor, helperinstance) helperinstance.addPrinter(printerinstance, "test") assert helperinstance._getCUPSQueueName( 'gcp://test/__test_printer') == "test"
def test_generateCUPSQueueName(): testdata = { "test": "test-test", "test1": "test1-test", "test-1": "test-1-test", "testing test": "testingtest-test", "testing£test": "testingtest-test", } requestor = CloudPrintRequestor() requestor.setAccount("test") for teststring in testdata: printerinstance = Printer( {"name": teststring, "id": "__test_printer", "capabilities": [{"name": "ns1:Colors", "type": "Feature"}]}, requestor, helperinstance, ) assert helperinstance.generateCUPSQueueName(printerinstance) == testdata[teststring]
def test_deletePrinter(): requestor = CloudPrintRequestor() requestor.setAccount("test") printerinstance = Printer( { 'name': 'Testing Printer', 'id': '__test_printer', 'capabilities': [{ 'name': 'ns1:Colors', 'type': 'Feature' }] }, requestor, helperinstance) helperinstance.addPrinter(printerinstance, "test") helperinstance.deletePrinter("printer that doesnt exist") assert len(helperinstance.getPrinters()) == 1 helperinstance.deletePrinter(printerinstance.getURI()) assert len(helperinstance.getPrinters()) == 0
def test_deletePrinter(): requestor = CloudPrintRequestor() requestor.setAccount("test") printerinstance = Printer( { "name": "Testing Printer", "id": "__test_printer", "capabilities": [{"name": "ns1:Colors", "type": "Feature"}], }, requestor, helperinstance, ) helperinstance.addPrinter(printerinstance, "test") helperinstance.deletePrinter("printer that doesnt exist") assert len(helperinstance.getPrinters()) == 1 helperinstance.deletePrinter(printerinstance.getURI()) assert len(helperinstance.getPrinters()) == 0
def test_renamePrinter(): assert len(helperinstance.getPrinters()) == 0 requestor = CloudPrintRequestor() requestor.setAccount("test") printerinstance = Printer( { "name": "Testing Printer", "id": "__test_printer", "capabilities": [{"name": "ns1:Colors", "type": "Feature"}], }, requestor, helperinstance, ) helperinstance.addPrinter(printerinstance, "test") helperinstance.renamePrinter("gcp://test/__test_printer", "Testing Printer 2", "test location") printerdetails = helperinstance.getPrinter("gcp://test/__test_printer") assert printerdetails["device-uri"] == "gcp://test/__test_printer" assert printerdetails["printer-info"] == "Testing Printer 2" assert printerdetails["printer-location"] == "test location" assert len(helperinstance.getPrinters()) == 1
def test_generateCUPSQueueName(): testdata = { 'test': 'test-test', 'test1': 'test1-test', 'test-1': 'test-1-test', 'testing test': 'testingtest-test', 'testing£test': 'testingtest-test', } requestor = CloudPrintRequestor() requestor.setAccount("test") for teststring in testdata: printerinstance = Printer( { 'name': teststring, 'id': '__test_printer', 'capabilities': [{ 'name': 'ns1:Colors', 'type': 'Feature' }] }, requestor, helperinstance) assert helperinstance.generateCUPSQueueName( printerinstance) == testdata[teststring]
def test_renamePrinter(): assert len(helperinstance.getPrinters()) == 0 requestor = CloudPrintRequestor() requestor.setAccount("test") printerinstance = Printer( { 'name': 'Testing Printer', 'id': '__test_printer', 'capabilities': [{ 'name': 'ns1:Colors', 'type': 'Feature' }] }, requestor, helperinstance) helperinstance.addPrinter(printerinstance, "test") helperinstance.renamePrinter('gcp://test/__test_printer', 'Testing Printer 2', 'test location') printerdetails = helperinstance.getPrinter('gcp://test/__test_printer') assert printerdetails['device-uri'] == 'gcp://test/__test_printer' assert printerdetails['printer-info'] == 'Testing Printer 2' assert printerdetails['printer-location'] == 'test location' assert len(helperinstance.getPrinters()) == 1
def SetupAuth(interactive=False, permissions=None, testUserIds=None): """Sets up requestors with authentication tokens Args: interactive: boolean, when set to true can prompt user, otherwise returns False if authentication fails Returns: requestor, storage: Authenticated requestors and an instance of storage """ if permissions is None: permissions = Auth.normal_permissions modifiedconfig = False # parse config file and extract useragents, which we use for account # names userids = [] if testUserIds is not None: userids = testUserIds if os.path.exists(Auth.config): data = json.loads(Utils.ReadFile(Auth.config)) if 'data' in data: for user in data['data']: userids.append(str(user['credential']['user_agent'])) else: Utils.WriteFile(Auth.config, '{}') Utils.FixFilePermissions(Auth.config) modifiedconfig = True if len(userids) == 0: userids = [None] requestors = [] storage = None credentials = None for userid in userids: if userid is not None: storage = multistore_file.get_credential_storage( Auth.config, Auth.clientid, userid, permissions) credentials = storage.get() if not credentials and interactive: credentials = Auth.AddAccount(storage, userid, permissions) modifiedconfig = True if userid is None: userid = credentials.user_agent if credentials: # renew if expired requestor = CloudPrintRequestor() if credentials.access_token_expired: Auth.RenewToken(interactive, requestor, credentials, storage, userid) requestor = credentials.authorize(requestor) requestor.setAccount(userid) requestors.append(requestor) # fix permissions if modifiedconfig: Utils.FixFilePermissions(Auth.config) if not credentials: return False, False else: return requestors, storage
def test_addGetPrinter(): assert len(helperinstance.getPrinters()) == 0 requestor = CloudPrintRequestor() requestor.setAccount("test") printerinstance = Printer( { "name": "Testing Printer", "id": "__test_printer", "capabilities": [{"name": "ns1:Colors", "type": "Feature"}], }, requestor, helperinstance, ) helperinstance.addPrinter(printerinstance, "test") printerdetails = helperinstance.getPrinter("gcp://test/__test_printer") assert printerdetails["device-uri"] == "gcp://test/__test_printer" assert helperinstance.getPrinter("invalid uri") is None assert len(helperinstance.getPrinters()) == 1 printerinstance2 = Printer( { "name": "Testing Printer 2", "id": "__test_printer_2", "capabilities": [{"name": "ns1:Colors", "type": "Feature"}], }, requestor, helperinstance, ) helperinstance.addPrinter(printerinstance2, "test2", "test-location", printerinstance.getPPDName()) printerdetails = helperinstance.getPrinter("gcp://test/__test_printer_2") assert printerdetails["device-uri"] == "gcp://test/__test_printer_2" assert printerdetails["printer-location"] == "test-location" assert len(helperinstance.getPrinters()) == 2 printerinstance3 = Printer( { "name": "Testing Printer 3", "id": "__test_printer_3", "capabilities": [{"name": "ns1:Colors", "type": "Feature"}], "tags": ["location=Test Location"], }, requestor, helperinstance, ) helperinstance.addPrinter(printerinstance3, "test3", "") printerdetails = helperinstance.getPrinter("gcp://test/__test_printer_3") assert printerdetails["device-uri"] == "gcp://test/__test_printer_3" assert printerdetails["printer-location"] == "Test Location" assert len(helperinstance.getPrinters()) == 3 # ensure coverage of errors printerinstance4 = Printer( { "name": "Testing Printer 4", "id": "__test_printer_4", "capabilities": [{"name": "ns1:Colors", "type": "Feature"}], "tags": ["location=Test Location"], }, requestor, helperinstance, ) helperinstance.addPrinter(printerinstance4, "test4", MockCUPS()) assert helperinstance.getPrinter("gcp://test/__test_printer_4") is None assert len(helperinstance.getPrinters()) == 3
def setup_function(function): global requestor requestor = CloudPrintRequestor()
def test_addGetPrinter(): assert len(helperinstance.getPrinters()) == 0 requestor = CloudPrintRequestor() requestor.setAccount("test") printerinstance = Printer( { 'name': 'Testing Printer', 'id': '__test_printer', 'capabilities': [{ 'name': 'ns1:Colors', 'type': 'Feature' }] }, requestor, helperinstance) helperinstance.addPrinter(printerinstance, "test") printerdetails = helperinstance.getPrinter('gcp://test/__test_printer') assert printerdetails['device-uri'] == 'gcp://test/__test_printer' assert helperinstance.getPrinter('invalid uri') is None assert len(helperinstance.getPrinters()) == 1 printerinstance2 = Printer( { 'name': 'Testing Printer 2', 'id': '__test_printer_2', 'capabilities': [{ 'name': 'ns1:Colors', 'type': 'Feature' }] }, requestor, helperinstance) helperinstance.addPrinter(printerinstance2, "test2", "test-location", printerinstance.getPPDName()) printerdetails = helperinstance.getPrinter('gcp://test/__test_printer_2') assert printerdetails['device-uri'] == 'gcp://test/__test_printer_2' assert printerdetails['printer-location'] == 'test-location' assert len(helperinstance.getPrinters()) == 2 printerinstance3 = Printer( { 'name': 'Testing Printer 3', 'id': '__test_printer_3', 'capabilities': [{ 'name': 'ns1:Colors', 'type': 'Feature' }], 'tags': ['location=Test Location'] }, requestor, helperinstance) helperinstance.addPrinter(printerinstance3, "test3", "") printerdetails = helperinstance.getPrinter('gcp://test/__test_printer_3') assert printerdetails['device-uri'] == 'gcp://test/__test_printer_3' assert printerdetails['printer-location'] == 'Test Location' assert len(helperinstance.getPrinters()) == 3 # ensure coverage of errors printerinstance4 = Printer( { 'name': 'Testing Printer 4', 'id': '__test_printer_4', 'capabilities': [{ 'name': 'ns1:Colors', 'type': 'Feature' }], 'tags': ['location=Test Location'] }, requestor, helperinstance) helperinstance.addPrinter(printerinstance4, "test4", MockCUPS()) assert helperinstance.getPrinter('gcp://test/__test_printer_4') is None assert len(helperinstance.getPrinters()) == 3