Skip to content

Commit

Permalink
test: add unit tests and coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
ReenigneArcher committed Dec 7, 2024
1 parent 1a5521d commit 18057d3
Show file tree
Hide file tree
Showing 7 changed files with 153 additions and 20 deletions.
42 changes: 41 additions & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,46 @@ concurrency:
cancel-in-progress: true

jobs:
pytest:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v4

- name: Set up Python
uses: actions/setup-python@v5
with:
python-version: 3.12

- name: Install Python Dependencies
run: |
python -m pip install --upgrade pip setuptools wheel
python -m pip install --upgrade -r requirements.txt
python -m pip install --upgrade -r requirements-dev.txt
- name: Test with pytest
id: test
shell: bash
run: |
python -m pytest \
-rxXs \
--tb=native \
--verbose \
--color=yes \
--cov=action \
tests
- name: Upload coverage
# any except canceled or skipped
if: >-
always() &&
(steps.test.outcome == 'success' || steps.test.outcome == 'failure') &&
startsWith(github.repository, 'LizardByte/')
uses: codecov/codecov-action@v4
with:
fail_ci_if_error: true
token: ${{ secrets.CODECOV_TOKEN }}

action:
runs-on: ubuntu-latest
steps:
Expand All @@ -33,7 +73,7 @@ jobs:
release:
if: github.event_name == 'push' && github.ref == 'refs/heads/master'
needs:
# - pytest
- pytest
- action
runs-on: ubuntu-latest
steps:
Expand Down
7 changes: 5 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
# facebook-post-action
[![GitHub Workflow Status (CI)](https://img.shields.io/github/actions/workflow/status/lizardbyte/facebook-post-action/ci.yml.svg?branch=master&label=CI%20build&logo=github&style=for-the-badge)](https://github.com/LizardByte/facebook-post-action/actions/workflows/ci.yml?query=branch%3Amaster)
[![Codecov](https://img.shields.io/codecov/c/gh/LizardByte/facebook-post-action.svg?token=LiDWK90cNO&style=for-the-badge&logo=codecov&label=codecov)](https://app.codecov.io/gh/LizardByte/facebook-post-action)

GitHub Action for posting to a facebook page or group.

## 🎒 Prep Work
Expand Down Expand Up @@ -128,5 +131,5 @@ To find your Page ID:

1. From News Feed, click Pages on the left side menu.
2. Click your Page's name to go to your Page.
3. Click About in the left column. If you don't see About in the left column, click See More.
4. Scroll down to find your Page ID below More Info.
3. Select the About tab. If you don't see the About tab, click the More dropdown.
4. Find your Page ID in the Page Transparency section.
38 changes: 21 additions & 17 deletions action/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,26 +8,24 @@

load_dotenv()

# inputs
ACCESS_TOKEN = os.environ['INPUT_ACCESS_TOKEN']
MESSAGE = os.environ['INPUT_MESSAGE']
PAGE_ID = os.environ['INPUT_PAGE_ID']
URL = os.getenv('INPUT_URL', None)
FAIL_ON_ERROR = os.getenv('INPUT_FAIL_ON_ERROR', 'true')

# constants
FACEBOOK_API_END = f'https://graph.facebook.com/{PAGE_ID}/feed'
def facebook_post(
access_token: str,
message: str,
page_id: str,
url: str = None,
fail_on_error: str = 'true'
):
facebook_api_end = f'https://graph.facebook.com/{page_id}/feed'


def main():
facebook_api_data = {
'message': MESSAGE,
'access_token': ACCESS_TOKEN,
'message': message,
'access_token': access_token,
}
if URL:
facebook_api_data['link'] = URL
if url:
facebook_api_data['link'] = url

r = requests.post(url=FACEBOOK_API_END, json=facebook_api_data)
r = requests.post(url=facebook_api_end, json=facebook_api_data)

result = r.json()

Expand All @@ -36,10 +34,16 @@ def main():
else:
print('Post error:')
print(result)
if FAIL_ON_ERROR.lower() == 'true':
if fail_on_error.lower() == 'true':
print('Failing the workflow')
sys.exit(1)


if __name__ == '__main__':
main() # pragma: no cover
facebook_post(
access_token=os.environ['INPUT_ACCESS_TOKEN'],
message=os.environ['INPUT_MESSAGE'],
page_id=os.environ['INPUT_PAGE_ID'],
url=os.getenv('INPUT_URL', None),
fail_on_error=os.getenv('INPUT_FAIL_ON_ERROR', 'true')
) # pragma: no cover
15 changes: 15 additions & 0 deletions codecov.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
---
codecov:
branch: master

coverage:
status:
project:
default:
target: auto
threshold: 10%

comment:
layout: "diff, flags, files"
behavior: default
require_changes: false # if true: only post the comment if coverage changes
2 changes: 2 additions & 0 deletions requirements-dev.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
pytest==8.3.3
pytest-cov==5.0.0
Empty file added tests/conftest.py
Empty file.
69 changes: 69 additions & 0 deletions tests/unit/test_main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
from unittest.mock import patch
from action.main import facebook_post


def test_facebook_post_success():
with patch('requests.post') as mock_post:
mock_post.return_value.json.return_value = {}

facebook_post(
access_token='test_access_token',
message='test_message',
page_id='test_page_id',
url='https://test.url',
fail_on_error='true'
)

mock_post.assert_called_once_with(
url='https://graph.facebook.com/test_page_id/feed',
json={
'message': 'test_message',
'access_token': 'test_access_token',
'link': 'https://test.url'
}
)


def test_facebook_post_error():
with patch('requests.post') as mock_post, patch('sys.exit') as mock_exit:
mock_post.return_value.json.return_value = {'error': 'test_error'}

facebook_post(
access_token='test_access_token',
message='test_message',
page_id='test_page_id',
url='https://test.url',
fail_on_error='true'
)

mock_post.assert_called_once_with(
url='https://graph.facebook.com/test_page_id/feed',
json={
'message': 'test_message',
'access_token': 'test_access_token',
'link': 'https://test.url'
}
)
mock_exit.assert_called_once_with(1)


def test_facebook_post_error_no_fail():
with patch('requests.post') as mock_post, patch('sys.exit') as mock_exit:
mock_post.return_value.json.return_value = {'error': 'test_error'}

facebook_post(
access_token='test_access_token',
message='test_message',
page_id='test_page_id',
url='https://test.url',
fail_on_error='false'
)
mock_post.assert_called_once_with(
url='https://graph.facebook.com/test_page_id/feed',
json={
'message': 'test_message',
'access_token': 'test_access_token',
'link': 'https://test.url'
}
)
mock_exit.assert_not_called()

0 comments on commit 18057d3

Please sign in to comment.