def gist_create(self, gist_pathes, description, secret=False): def load_file(fname, path='.'): return open(os.path.join(path, fname), 'r') gist_files = dict() for gist_path in gist_pathes: if not os.path.isdir(gist_path): gist_files[os.path.basename(gist_path)] = load_file(gist_path) else: for gist_file in os.listdir(gist_path): if not os.path.isdir(os.path.join(gist_path, gist_file)) and not gist_file.startswith('.'): gist_files[gist_file] = load_file(gist_file, gist_path) try: snippet = Snippet.create( files=gist_files, payload=SnippetPayload( payload=dict( title=description, scm=RepositoryType.GIT, is_private=secret ) ), client=self.bb.client ) return snippet.links['html']['href'] except HTTPError as err: raise ResourceError("Couldn't create snippet: {}".format(err)) from err
class SnippetPayloadFixture(SnippetFixture): builder = SnippetPayload() # GIVEN: a class under test class_under_test = 'SnippetPayload' # GIVEN: An example object created from example data @classmethod def example_object(cls): return SnippetPayload(json.loads(cls.resource_data()))
def test_response_is_a_snippet(self): httpretty.register_uri(httpretty.PUT, self.url, content_type='application/json', body=self.resource_data(), status=200) payload = SnippetPayload() \ .add_title(self.title) \ .add_scm(self.scm) \ .add_is_private(self.is_private) response = self.example_object().modify(payload=payload) content_type = httpretty.last_request().headers.get('Content-Type') assert content_type.startswith('application/json') assert isinstance(response, Snippet)
def test_response_is_a_snippet(self): httpretty.register_uri(httpretty.POST, self.url, content_type='application/json', body=self.resource_data(), status=200) payload = SnippetPayload() \ .add_title(self.title) \ .add_scm(self.scm) \ .add_is_private(self.is_private) response = Snippet.create(self.files, payload, client=self.test_client) content_type = httpretty.last_request().headers.get('Content-Type') assert content_type.startswith('multipart/form-data') assert isinstance(response, Snippet)
def test_response_is_a_snippet_with_two_files(self): httpretty.register_uri(httpretty.POST, self.url, content_type='application/json', body=self.resource_data('Snippet.two_files'), status=200) payload = SnippetPayload() \ .add_title(self.title) \ .add_scm(self.scm) \ .add_is_private(self.is_private) response = Snippet.create(self.files, payload, client=self.test_client) content_type = httpretty.last_request().headers.get('Content-Type') assert content_type.startswith('multipart/form-data') assert isinstance(response, Snippet) # I would expect the JSON to be ordered # but somewhere that order was getting lost. expected = ['example_upload_1.txt', 'example_upload_2.rst'] assert sorted(expected) == sorted(response.filenames)
def example_object(cls): return SnippetPayload(json.loads(cls.resource_data()))
def setup_class(cls): cls.payload = SnippetPayload() \ .add_title(cls.title) \ .add_scm(cls.scm) \ .add_is_private(cls.is_private) cls.expected = json.loads(cls.resource_data('SnippetPayload.full'))