Skip to content

Commit

Permalink
Merge pull request #5 from kilobyteno/release-0.1.1
Browse files Browse the repository at this point in the history
  • Loading branch information
dsbilling authored Sep 16, 2023
2 parents 0474022 + 625e68a commit 4745e68
Show file tree
Hide file tree
Showing 8 changed files with 98 additions and 26 deletions.
8 changes: 4 additions & 4 deletions .env.example
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
ENV=staging
OPPE_ENV=staging

TEST_API_TOKEN=
TEST_PROJECT_ID=
TEST_CHANNEL_ID=
OPPE_TEST_API_TOKEN=
OPPE_TEST_PROJECT_ID=
OPPE_TEST_CHANNEL_ID=
12 changes: 8 additions & 4 deletions .github/workflows/code-style.yml
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,13 @@ jobs:
run: mv .env.example .env
- name: Change environment variables in .env
run: |
sed -i "s/TEST_API_TOKEN=/TEST_API_TOKEN=${{ secrets.TEST_API_TOKEN }}/g" .env
sed -i "s/TEST_PROJECT_ID=/TEST_PROJECT_ID=${{ secrets.TEST_PROJECT_ID }}/g" .env
sed -i "s/TEST_CHANNEL_ID=/TEST_CHANNEL_ID=${{ secrets.TEST_CHANNEL_ID }}/g" .env
sed -i "s/OPPE_TEST_API_TOKEN=/OPPE_TEST_API_TOKEN=${{ secrets.OPPE_TEST_API_TOKEN }}/g" .env
sed -i "s/OPPE_TEST_PROJECT_ID=/OPPE_TEST_PROJECT_ID=${{ secrets.OPPE_TEST_PROJECT_ID }}/g" .env
sed -i "s/OPPE_TEST_CHANNEL_ID=/OPPE_TEST_CHANNEL_ID=${{ secrets.OPPE_TEST_CHANNEL_ID }}/g" .env
- name: Run Tests
run: |
pytest --disable-pytest-warnings
pytest --disable-pytest-warnings --cov --cov-report=xml --cov-report=term-missing
- name: Upload coverage reports to Codecov
uses: codecov/codecov-action@v3
env:
CODECOV_TOKEN: ${{ secrets.CODECOV_TOKEN }}
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
# oppe-for-python
[![codecov](https://codecov.io/gh/kilobyteno/oppe-for-python/graph/badge.svg?token=JKLDG13D1W)](https://codecov.io/gh/kilobyteno/oppe-for-python)
[![PyPI version](https://badge.fury.io/py/oppe.svg)](https://badge.fury.io/py/oppe)
[![Downloads](https://pepy.tech/badge/oppe)](https://pepy.tech/project/oppe)
[![License](https://img.shields.io/github/license/kilobyteno/oppe-for-python)](LICENSE)

An API wrapper for [Oppe](https://oppe.app) written in Python.


Expand Down
2 changes: 1 addition & 1 deletion oppe/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class Config:
if os.path.exists(dotenv_path):
load_dotenv(dotenv_path)

ENV = os.getenv('ENV')
ENV = os.getenv('OPPE_ENV')
if ENV == 'staging':
BASE_URL = 'https://staging.oppe.app/api'

Expand Down
19 changes: 12 additions & 7 deletions oppe/oppe.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,19 +55,24 @@ def event(self, channel_id, title, description=None, emoji=None, data=None):
if not validate_uuid(uuid_input=channel_id):
raise UuidValidationError(msg='Channel ID must be a valid UUID')

# Make sure data is an object
if not data:
data = {}

# Create payload to send to Oppe
payload = {
'channel_id': channel_id,
'title': title,
'description': description,
'emoji': emoji,
'data': json.dumps(data),
}

# If description is not None, add it to the payload
if description:
payload['description'] = description

# If emoji is not None, add it to the payload
if emoji:
payload['emoji'] = emoji

# If data is not None, add it to the payload
if data:
payload['data'] = json.dumps(data)

# Send request to Oppe
response = requests.post(Config.EVENT_URL, data=json.dumps(payload), headers=request_header(api_token=self.api_token))
if response.status_code != 201:
Expand Down
1 change: 1 addition & 0 deletions requirements-dev.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,4 @@ Faker~=19.6.1
pytest~=7.4.0
pytest-cov~=4.1.0
pytest-sugar~=0.9.7
pytest-mock~=3.11.1
42 changes: 42 additions & 0 deletions tests/test_exceptions.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
from oppe.exceptions import EventRequestError, OppeError, UuidValidationError


def test_default_parameters():
""" Test default parameters """
error = OppeError()
assert error.msg == 'Unknown error occurred'
assert error.data is None


def test_custom_parameters():
""" Test custom parameters """
error = OppeError(msg='Custom error message', data={'key': 'value'})
assert error.msg == 'Custom error message'
assert error.data == {'key': 'value'}


def test_empty_string_message():
""" Test empty string message """
error = OppeError('')
assert error.msg == ''
assert error.data is None


def test_with_only_msg_attribute():
""" Test with only msg attribute """
error = OppeError('Error message')
assert str(error) == 'Error message'


def test_subclass_uuid_validation_error():
""" Test subclass UuidValidationError """
error = UuidValidationError(msg='Uuid Validation Error')
assert error.msg == 'Uuid Validation Error'
assert error.data is None


def test_subclass_event_request_error():
""" Test subclass EventRequestError """
error = EventRequestError(msg='Event Request Error')
assert error.msg == 'Event Request Error'
assert error.data is None
35 changes: 25 additions & 10 deletions tests/test_oppe.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import os
from unittest.mock import Mock

import pytest
from dotenv import load_dotenv
from faker import Faker

from oppe.exceptions import UuidValidationError
from oppe.exceptions import EventRequestError, UuidValidationError
from oppe.oppe import Oppe

fake = Faker()
Expand All @@ -16,62 +18,62 @@

def init_oppe():
""" Initialize Oppe """
return Oppe(api_token=os.getenv('TEST_API_TOKEN'), project_id=os.getenv('TEST_PROJECT_ID'))
return Oppe(api_token=os.getenv('OPPE_TEST_API_TOKEN'), project_id=os.getenv('OPPE_TEST_PROJECT_ID'))


def test_publish_event():
""" Test publish event """
oppe = init_oppe()
response = oppe.event(channel_id=os.getenv('TEST_CHANNEL_ID'), title=fake.domain_word(), description=fake.sentence())
response = oppe.event(channel_id=os.getenv('OPPE_TEST_CHANNEL_ID'), title=fake.domain_word(), description=fake.sentence())
assert response is True


def test_publish_event_with_emoji():
""" Test publish event with emoji """
oppe = init_oppe()
response = oppe.event(channel_id=os.getenv('TEST_CHANNEL_ID'), title=fake.domain_word(), description=fake.sentence(), emoji='πŸ‘‹')
response = oppe.event(channel_id=os.getenv('OPPE_TEST_CHANNEL_ID'), title=fake.domain_word(), description=fake.sentence(), emoji='πŸ‘‹')
assert response is True


def test_publish_event_with_data():
""" Test publish event with data """
oppe = init_oppe()
response = oppe.event(channel_id=os.getenv('TEST_CHANNEL_ID'), title=fake.domain_word(), description=fake.sentence(), data={'user_id': 1})
response = oppe.event(channel_id=os.getenv('OPPE_TEST_CHANNEL_ID'), title=fake.domain_word(), description=fake.sentence(), data={'user_id': 1})
assert response is True


def test_publish_event_with_emoji_and_data():
""" Test publish event with emoji and data """
oppe = init_oppe()
response = oppe.event(channel_id=os.getenv('TEST_CHANNEL_ID'), title=fake.domain_word(), description=fake.sentence(), emoji='πŸ‘‹', data={'user_id': 1})
response = oppe.event(channel_id=os.getenv('OPPE_TEST_CHANNEL_ID'), title=fake.domain_word(), description=fake.sentence(), emoji='πŸ‘‹', data={'user_id': 1})
assert response is True


def test_publish_event_with_emoji_and_data_and_no_description():
""" Test publish event with emoji and data and no description """
oppe = init_oppe()
response = oppe.event(channel_id=os.getenv('TEST_CHANNEL_ID'), title=fake.domain_word(), emoji='πŸ‘‹', data={'user_id': 1})
response = oppe.event(channel_id=os.getenv('OPPE_TEST_CHANNEL_ID'), title=fake.domain_word(), emoji='πŸ‘‹', data={'user_id': 1})
assert response is True


def test_publish_event_with_emoji_and_no_description():
""" Test publish event with emoji and no description """
oppe = init_oppe()
response = oppe.event(channel_id=os.getenv('TEST_CHANNEL_ID'), title=fake.domain_word(), emoji='πŸ‘‹')
response = oppe.event(channel_id=os.getenv('OPPE_TEST_CHANNEL_ID'), title=fake.domain_word(), emoji='πŸ‘‹')
assert response is True


def test_publish_event_with_data_and_no_description():
""" Test publish event with data and no description """
oppe = init_oppe()
response = oppe.event(channel_id=os.getenv('TEST_CHANNEL_ID'), title=fake.domain_word(), data={'user_id': 1})
response = oppe.event(channel_id=os.getenv('OPPE_TEST_CHANNEL_ID'), title=fake.domain_word(), data={'user_id': 1})
assert response is True


def test_publish_event_with_no_description():
""" Test publish event with no description """
oppe = init_oppe()
response = oppe.event(channel_id=os.getenv('TEST_CHANNEL_ID'), title=fake.domain_word())
response = oppe.event(channel_id=os.getenv('OPPE_TEST_CHANNEL_ID'), title=fake.domain_word())
assert response is True


Expand All @@ -82,3 +84,16 @@ def test_publish_event_with_wrong_channel_id():
oppe.event(channel_id=fake.sha256(), title=fake.domain_word())
except UuidValidationError as e:
assert e.msg == 'Channel ID must be a valid UUID'


def test_invalid_api_token(mocker):
""" Test invalid api token """
# Mock the requests.post method to return a non-201 response
mocker.patch('requests.post', return_value=Mock(status_code=401, json=lambda: {'error': 'Unauthorized'}))

# Create an instance of the Oppe class
oppe = Oppe(api_token='invalid_token', project_id=os.getenv('OPPE_TEST_PROJECT_ID'))

# Call the event method with valid parameters
with pytest.raises(EventRequestError):
oppe.event(channel_id=os.getenv('OPPE_TEST_CHANNEL_ID'), title=fake.domain_word())

0 comments on commit 4745e68

Please sign in to comment.