def test_should_upload_dists(self, mock_token):
        # Create temporary file to upload
        dummy_file_path = os.path.join(temp_dir, 'dist', 'testupload.md')
        os.makedirs(os.path.dirname(dummy_file_path), exist_ok=True)
        dummy_content = '# Test File\n\n*Dummy asset for testing uploads.*'
        with open(dummy_file_path, 'w') as dummy_file:
            dummy_file.write(dummy_content)

        def request_callback(request):
            self.assertEqual(request.body.decode().replace('\r\n', '\n'),
                             dummy_content)
            self.assertEqual(request.url, self.dist_asset_url_params)
            self.assertEqual(request.headers['Content-Type'], 'text/markdown')
            self.assertEqual('token super-token',
                             request.headers['Authorization'])

            return 201, {}, json.dumps({})

        responses.add(responses.GET,
                      self.get_url,
                      status=200,
                      body='{"id": 1}',
                      content_type='application/json')
        responses.add_callback(responses.POST,
                               self.asset_url,
                               callback=request_callback)
        status = Github.upload_dists('relekang', 'rmoq', '1.0.0',
                                     os.path.dirname(dummy_file_path))
        self.assertTrue(status)

        # Remove test file
        os.remove(dummy_file_path)
    def test_should_upload_asset_with_no_extension(self, mock_token):
        # Create temporary file to upload
        dummy_file_path = os.path.join(temp_dir, "testupload-no-extension")
        os.makedirs(os.path.dirname(dummy_file_path), exist_ok=True)
        dummy_content = (
            "# Test File with no extension\n\n*Dummy asset for testing uploads.*"
        )
        with open(dummy_file_path, "w") as dummy_file:
            dummy_file.write(dummy_content)

        def request_callback(request):
            self.assertEqual(request.body.decode().replace("\r\n", "\n"),
                             dummy_content)
            self.assertEqual(request.url, self.asset_no_extension_url_params)
            self.assertEqual(request.headers["Content-Type"],
                             "application/octet-stream")
            self.assertEqual("token super-token",
                             request.headers["Authorization"])

            return 201, {}, json.dumps({})

        responses.add_callback(responses.POST,
                               self.asset_url,
                               callback=request_callback)
        status = Github.upload_asset("relekang", "rmoq", 1, dummy_file_path,
                                     "Dummy file no extension")
        self.assertTrue(status)

        # Remove test file
        os.remove(dummy_file_path)
    def test_should_upload_dists(self, mock_token):
        # Create temporary file to upload
        dummy_file_path = os.path.join(temp_dir, "dist", "testupload.md")
        os.makedirs(os.path.dirname(dummy_file_path), exist_ok=True)
        dummy_content = "# Test File\n\n*Dummy asset for testing uploads.*"
        with open(dummy_file_path, "w") as dummy_file:
            dummy_file.write(dummy_content)

        def request_callback(request):
            self.assertEqual(request.body.decode().replace("\r\n", "\n"),
                             dummy_content)
            self.assertEqual(request.url, self.dist_asset_url_params)
            self.assertEqual(request.headers["Content-Type"], "text/markdown")
            self.assertEqual("token super-token",
                             request.headers.get("Authorization"))

            return 201, {}, json.dumps({})

        responses.add(
            responses.GET,
            self.get_url,
            status=200,
            body='{"id": 1}',
            content_type="application/json",
        )
        responses.add_callback(responses.POST,
                               self.asset_url,
                               callback=request_callback)
        status = Github.upload_dists("relekang", "rmoq", "1.0.0",
                                     os.path.dirname(dummy_file_path))
        self.assertTrue(status)

        # Remove test file
        os.remove(dummy_file_path)
 def test_should_return_false_status_if_it_failed(self):
     responses.add(
         responses.POST,
         self.url,
         status=400,
         body="{}",
         content_type="application/json",
     )
     responses.add(
         responses.GET,
         self.get_url,
         status=200,
         body='{"id": 1}',
         content_type="application/json",
     )
     responses.add(
         responses.POST,
         self.edit_url,
         status=400,
         body="{}",
         content_type="application/json",
     )
     self.assertFalse(
         Github.post_release_changelog("relekang", "rmoq", "1.0.0", "text")
     )
    def test_should_post_changelog_using_netrc(self, mock_token):
        with NamedTemporaryFile("w") as netrc_file:
            netrc_file.write("machine api.github.com\n")
            netrc_file.write("login username\n")
            netrc_file.write("password password\n")

            netrc_file.flush()

            def request_callback(request):
                payload = json.loads(request.body)
                self.assertEqual(payload["tag_name"], "v1.0.0")
                self.assertEqual(payload["body"], "text")
                self.assertEqual(payload["draft"], False)
                self.assertEqual(payload["prerelease"], False)
                self.assertEqual(
                    "Basic " + base64.encodebytes(b"username:password").decode(
                        "ascii").strip(),
                    request.headers.get("Authorization"),
                )

                return 201, {}, json.dumps({})

            responses.add_callback(
                responses.POST,
                self.url,
                callback=request_callback,
                content_type="application/json",
            )

            with mock.patch.dict(os.environ, {"NETRC": netrc_file.name}):
                status = Github.post_release_changelog("relekang", "rmoq",
                                                       "1.0.0", "text")
                self.assertTrue(status)
    def test_should_upload_asset(self, mock_token):
        # Create temporary file to upload
        dummy_file_path = '/tmp/testupload.md'
        dummy_content = '# Test File\n\n*Dummy asset for testing uploads.*'
        with open(dummy_file_path, 'w') as dummy_file:
            dummy_file.write(dummy_content)

        def request_callback(request):
            self.assertEqual(request.body.decode(), dummy_content)
            self.assertEqual(request.url, self.asset_url_params)
            self.assertEqual(request.headers['Content-Type'], 'text/markdown')
            self.assertEqual('token super-token', request.headers['Authorization'])

            return 201, {}, json.dumps({})

        responses.add_callback(
            responses.POST,
            self.asset_url,
            callback=request_callback
        )
        status = Github.upload_asset(
            'relekang', 'rmoq', 1, dummy_file_path, 'Dummy file')
        self.assertTrue(status)

        # Remove test file
        os.remove(dummy_file_path)
 def test_should_return_true_if_success(self):
     responses.add(responses.GET,
                   self.url,
                   body=self.get_response('success'),
                   content_type='application/json')
     self.assertTrue(
         Github.check_build_status(
             'relekang', 'rmoq',
             '6dcb09b5b57875f334f61aebed695e2e4193db5e'))
 def test_should_return_true_if_success(self):
     responses.add(
         responses.GET,
         self.url,
         body=self.get_response('success'),
         content_type='application/json'
     )
     self.assertTrue(Github.check_build_status('relekang', 'rmoq',
                                               '6dcb09b5b57875f334f61aebed695e2e4193db5e'))
 def test_should_return_true_if_success(self):
     responses.add(
         responses.GET,
         self.url,
         body=self.get_response("success"),
         content_type="application/json",
     )
     self.assertTrue(
         Github.check_build_status(
             "relekang", "rmoq",
             "6dcb09b5b57875f334f61aebed695e2e4193db5e"))
 def test_should_return_false_status_if_it_failed(self):
     responses.add(responses.POST,
                   self.url,
                   status=400,
                   body='{}',
                   content_type='application/json')
     responses.add(responses.GET,
                   self.get_url,
                   status=200,
                   body='{"id": 1}',
                   content_type='application/json')
     responses.add(responses.POST,
                   self.edit_url,
                   status=400,
                   body='{}',
                   content_type='application/json')
     self.assertFalse(
         Github.post_release_changelog('relekang', 'rmoq', '1.0.0', 'text'))
    def test_should_post_changelog(self, mock_token):
        def request_callback(request):
            payload = json.loads(request.body)
            self.assertEqual(payload['tag_name'], 'v1.0.0')
            self.assertEqual(payload['body'], 'text')
            self.assertEqual(payload['draft'], False)
            self.assertEqual(payload['prerelease'], False)
            self.assertEqual('token super-token',
                             request.headers['Authorization'])

            return 201, {}, json.dumps({})

        responses.add_callback(responses.POST,
                               self.url,
                               callback=request_callback,
                               content_type='application/json')
        status = Github.post_release_changelog('relekang', 'rmoq', '1.0.0',
                                               'text')
        self.assertTrue(status)
    def test_should_post_changelog(self):
        def request_callback(request):
            payload = json.loads(request.body)
            self.assertEqual(payload['tag_name'], 'v1.0.0')
            self.assertEqual(payload['body'], 'text')
            self.assertEqual(payload['draft'], False)
            self.assertEqual(payload['prerelease'], False)
            self.assertIn('?access_token=super-token', request.url)

            return 201, {}, json.dumps({})

        responses.add_callback(
            responses.POST,
            self.url,
            callback=request_callback,
            content_type='application/json'
        )
        status, payload = Github.post_release_changelog('relekang', 'rmoq', '1.0.0', 'text')
        self.assertTrue(status)
        self.assertEqual(payload, {})
    def test_should_post_changelog(self, mock_token):
        def request_callback(request):
            payload = json.loads(request.body)
            self.assertEqual(payload["tag_name"], "v1.0.0")
            self.assertEqual(payload["body"], "text")
            self.assertEqual(payload["draft"], False)
            self.assertEqual(payload["prerelease"], False)
            self.assertEqual("token super-token",
                             request.headers["Authorization"])

            return 201, {}, json.dumps({})

        responses.add_callback(
            responses.POST,
            self.url,
            callback=request_callback,
            content_type="application/json",
        )
        status = Github.post_release_changelog("relekang", "rmoq", "1.0.0",
                                               "text")
        self.assertTrue(status)
 def test_should_return_false_status_if_it_failed(self):
     responses.add(
         responses.POST,
         self.url,
         status=400,
         body='{}',
         content_type='application/json'
     )
     responses.add(
         responses.GET,
         self.get_url,
         status=200,
         body='{"id": 1}',
         content_type='application/json'
     )
     responses.add(
         responses.POST,
         self.edit_url,
         status=400,
         body='{}',
         content_type='application/json'
     )
     self.assertFalse(Github.post_release_changelog('relekang', 'rmoq', '1.0.0', 'text')[0])
def test_github_token():
    with mock.patch('os.environ', {'GH_TOKEN': 'token'}):
        assert (Github.token() == 'token')
def test_github_token():
    with mock.patch("os.environ", {"GH_TOKEN": "token"}):
        assert Github.token() == "token"