def test_StdInToTempFile(): testdata = 'oux3ooquoideereeng5A' mockstdin = open('/tmp/stdintest', 'wb') mockstdin.write(testdata) mockstdin.close() mockstdin = open('/tmp/stdintest', 'r') tempfile = Utils.StdInToTempFile(1, 'testuser', stdin=mockstdin) assert Utils.ReadFile(tempfile) == testdata mockstdin.close() os.unlink('/tmp/stdintest') os.unlink(tempfile)
def test_base64encode(): Utils.WriteFile('/tmp/testfile', 'data') == True assert Utils.Base64Encode('/tmp/testfile') == '/tmp/testfile.b64' assert Utils.ReadFile( '/tmp/testfile.b64') == 'data:application/octet-stream;base64,ZGF0YQ==' os.unlink('/tmp/testfile.b64') os.mkdir('/tmp/testfile.b64') assert Utils.Base64Encode('/tmp/testfile') is None os.unlink('/tmp/testfile') os.rmdir('/tmp/testfile.b64') assert Utils.Base64Encode('/tmp/testfile/dsiahdisa') is None
def test_setupAuth(): testUserName = '******' # ensure setup with no details doesnt create file assert os.path.exists(Auth.config) is False assert Auth.SetupAuth(False) == (False, False) assert os.path.exists(Auth.config) is True assert Utils.ReadFile(Auth.config) == "{}" os.unlink(Auth.config) # create initial file assert os.path.exists(Auth.config) is False assert Auth.SetupAuth(False, testUserIds=['test']) == (False, False) assert os.path.exists(Auth.config) is True # ensure permissions are correct after creating config assert '0660' == oct(os.stat(Auth.config)[stat.ST_MODE])[-4:] # add dummy details storage = multistore_file.get_credential_storage( Auth.config, Auth.clientid, 'testuseraccount', ['https://www.googleapis.com/auth/cloudprint']) credentials = client.OAuth2Credentials( 'test', Auth.clientid, 'testsecret', 'testtoken', 1, 'https://www.googleapis.com/auth/cloudprint', testUserName) storage.put(credentials) # ensure permissions are correct after populating config assert '0660' == oct(os.stat(Auth.config)[stat.ST_MODE])[-4:] # re-run to test getting credentials requestors, storage = Auth.SetupAuth(False) assert requestors is not None assert storage is not None # check deleting account assert Auth.DeleteAccount(testUserName) is None requestors, storage = Auth.SetupAuth(False) assert requestors is False assert storage is False
def test_readFile(): Utils.WriteFile('/tmp/testfile', 'data') assert Utils.ReadFile('/tmp/testfile') == 'data' assert Utils.ReadFile('/tmp/filethatdoesntexist') is None os.unlink('/tmp/testfile')
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