def test_base64encode(self): self.assertEqual(utils.base64encode('foo'), 'Zm9v') self.assertEqual(utils.base64encode(u'I ♡ Python'), 'SSDimaEgUHl0aG9u') if sys.version_info >= (3, 0): binary_content = b'\x01\x02\x99' else: binary_content = '\x01\x02\x99' self.assertEqual(utils.base64encode(binary_content), 'AQKZ')
def test_snapshot(self, mock): root_dir = os.path.join(TEST_FILES_DIR, 'static') webdriver = FakeWebdriver() loader = percy.ResourceLoader(root_dir=root_dir, base_url='/assets/', webdriver=webdriver) config = percy.Config(access_token='foo') runner = percy.Runner(config=config, loader=loader) response_text = json.dumps(SIMPLE_BUILD_FIXTURE) mock.post('https://percy.io/api/v1/builds/', text=response_text) runner.initialize_build() # Plain snapshot without a missing resource. response_text = json.dumps(SIMPLE_SNAPSHOT_FIXTURE) mock.post('https://percy.io/api/v1/builds/123/snapshots/', text=response_text) mock.post('https://percy.io/api/v1/snapshots/256/finalize', text='{"success": true}') runner.snapshot() # Snapshot with a missing resource. response_text = json.dumps({ 'data': { 'id': '256', 'type': 'snapshots', 'relationships': { 'self': '/api/v1/snapshots/256', 'missing-resources': { 'data': [ { 'type': 'resources', 'id': loader.snapshot_resources[0].sha, }, ], }, }, }, }) mock.post('https://percy.io/api/v1/builds/123/snapshots/', text=response_text) mock.post('https://percy.io/api/v1/builds/123/resources/', text='{"success": true}') mock.post('https://percy.io/api/v1/snapshots/256/finalize', text='{"success": true}') runner.snapshot(name='foo', enable_javascript=True, widths=[1280]) # Assert that kwargs are passed through correctly to create_snapshot. assert mock.request_history[3].json()['data']['attributes'] == { 'enable-javascript': True, 'name': 'foo', 'widths': [1280], } # Assert that the snapshot resource was uploaded correctly. assert mock.request_history[4].json() == { 'data': { 'type': 'resources', 'id': loader.snapshot_resources[0].sha, 'attributes': { 'base64-content': utils.base64encode(FakeWebdriver.page_source) }, }, }
def test_snapshot(self, mock): root_dir = os.path.join(TEST_FILES_DIR, 'static') webdriver = FakeWebdriver() loader = percy.ResourceLoader(root_dir=root_dir, base_url='/assets/', webdriver=webdriver) config = percy.Config(access_token='foo') runner = percy.Runner(config=config, loader=loader) response_text = json.dumps(SIMPLE_BUILD_FIXTURE) mock.post('https://percy.io/api/v1/repos/foo/bar/builds/', text=response_text) runner.initialize_build(repo='foo/bar') # Plain snapshot without a missing resource. response_text = json.dumps(SIMPLE_SNAPSHOT_FIXTURE) mock.post('https://percy.io/api/v1/builds/123/snapshots/', text=response_text) mock.post('https://percy.io/api/v1/snapshots/256/finalize', text='{"success": true}') runner.snapshot() # Snapshot with a missing resource. response_text = json.dumps({ 'data': { 'id': '256', 'type': 'snapshots', 'relationships': { 'self': '/api/v1/snapshots/256', 'missing-resources': { 'data': [ { 'type': 'resources', 'id': loader.snapshot_resources[0].sha, }, ], }, }, }, }) mock.post('https://percy.io/api/v1/builds/123/snapshots/', text=response_text) mock.post('https://percy.io/api/v1/builds/123/resources/', text='{"success": true}') mock.post('https://percy.io/api/v1/snapshots/256/finalize', text='{"success": true}') runner.snapshot(name='foo', enable_javascript=True, widths=[1280]) # Assert that kwargs are passed through correctly to create_snapshot. assert mock.request_history[3].json()['data']['attributes'] == { 'enable-javascript': True, 'name': 'foo', 'widths': [1280], } # Assert that the snapshot resource was uploaded correctly. assert mock.request_history[4].json() == { 'data': { 'type': 'resources', 'id': loader.snapshot_resources[0].sha, 'attributes': { 'base64-content': utils.base64encode(FakeWebdriver.page_source) }, }, }
def upload_resource(self, build_id, content): sha = utils.sha256hash(content) data = { 'data': { 'type': 'resources', 'id': sha, 'attributes': { 'base64-content': utils.base64encode(content), } } } path = "{base_url}/builds/{build_id}/resources/".format( base_url=self.config.api_url, build_id=build_id) return self._connection.post(path=path, data=data)
def upload_resource(self, build_id, content): sha = utils.sha256hash(content) data = { 'data': { 'type': 'resources', 'id': sha, 'attributes': { 'base64-content': utils.base64encode(content), } } } path = "{base_url}/builds/{build_id}/resources/".format( base_url=self.config.api_url, build_id=build_id ) return self._connection.post(path=path, data=data)
def test_upload_resource(self, mock): mock.post('https://percy.io/api/v1/builds/123/resources/', text='{"success": "true"}') content = 'foo' result = self.percy_client.upload_resource(build_id=123, content=content) assert mock.request_history[0].json() == { 'data': { 'type': 'resources', 'id': utils.sha256hash(content), 'attributes': { 'base64-content': utils.base64encode(content) } } } assert result == {'success': 'true'}
def test_upload_resource(self, mock): mock.post('https://percy.io/api/v1/builds/123/resources/', text='{"success": "true"}') content = 'foo' result = self.percy_client.upload_resource(build_id=123, content=content) assert mock.request_history[0].json() == { 'data': { 'type': 'resources', 'id': utils.sha256hash(content), 'attributes': { 'base64-content': utils.base64encode(content) } } } assert result == {'success': 'true'}
def test_initialize_build_sends_missing_resources(self, mock): root_dir = os.path.join(TEST_FILES_DIR, 'static') loader = percy.ResourceLoader(root_dir=root_dir, base_url='/assets/') config = percy.Config(access_token='foo') runner = percy.Runner(config=config, loader=loader) build_fixture = { 'data': { 'id': '123', 'type': 'builds', 'relationships': { 'self': "/api/v1/snapshots/123", 'missing-resources': { 'data': [ { 'type': 'resources', 'id': loader.build_resources[0].sha, }, ], }, }, }, } mock.post('https://percy.io/api/v1/repos/foo/bar/builds/', text=json.dumps(build_fixture)) mock.post('https://percy.io/api/v1/builds/123/resources/', text='{"success": true}') runner.initialize_build(repo='foo/bar') # Make sure the missing resources were uploaded. The mock above will not fail if not called. with open(loader.build_resources[0].local_path, 'r') as f: content = f.read() assert len(content) > 0 assert mock.request_history[1].json() == { 'data': { 'type': 'resources', 'id': loader.build_resources[0].sha, 'attributes': { 'base64-content': utils.base64encode(content) }, }, }
def test_initialize_build_sends_missing_resources(self, mock): root_dir = os.path.join(TEST_FILES_DIR, 'static') loader = percy.ResourceLoader(root_dir=root_dir, base_url='/assets/') config = percy.Config(access_token='foo') runner = percy.Runner(config=config, loader=loader) build_fixture = { 'data': { 'id': '123', 'type': 'builds', 'relationships': { 'self': "/api/v1/snapshots/123", 'missing-resources': { 'data': [ { 'type': 'resources', 'id': loader.build_resources[0].sha, }, ], }, }, }, } mock.post('https://percy.io/api/v1/repos/foo/bar/builds/', text=json.dumps(build_fixture)) mock.post('https://percy.io/api/v1/builds/123/resources/', text='{"success": true}') runner.initialize_build(repo='foo/bar') # Make sure the missing resources were uploaded. The mock above will not fail if not called. with open(loader.build_resources[0].local_path, 'r') as f: content = f.read() assert len(content) > 0 assert mock.request_history[1].json() == { 'data': { 'type': 'resources', 'id': loader.build_resources[0].sha, 'attributes': { 'base64-content': utils.base64encode(content) }, }, }