Skip to content

Commit

Permalink
ANXOS-171: Extend worflows and adapt module, reformat and extend tests
Browse files Browse the repository at this point in the history
  • Loading branch information
nezhar committed Jun 22, 2022
1 parent de25af5 commit 1a73d6c
Show file tree
Hide file tree
Showing 16 changed files with 329 additions and 100 deletions.
41 changes: 41 additions & 0 deletions .github/workflows/publish.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
name: Publish package
on:
release:
types: [created]

jobs:
deploy:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v2

- name: Set up Python
uses: actions/setup-python@v2
with:
python-version: '3.8'
architecture: 'x64'

- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install setuptools wheel twine
- name: Build source and binary distribution package
run: |
python setup.py sdist bdist_wheel
env:
PACKAGE_VERSION: ${{ github.ref }}

- name: Check distribution package
run: |
twine check dist/*
- name: Publish distribution package
run: |
twine upload dist/*
env:
TWINE_REPOSITORY: ${{ secrets.PYPI_REPOSITORY }}
TWINE_USERNAME: ${{ secrets.PYPI_USERNAME }}
TWINE_PASSWORD: ${{ secrets.PYPI_PASSWORD }}
TWINE_NON_INTERACTIVE: yes
76 changes: 76 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
name: Run linter and tests
on: [push, pull_request]

jobs:
test:
runs-on: ubuntu-latest
strategy:
fail-fast: false
matrix:
python-version:
- "3.7"
- "3.8"
- "3.9"
- "3.10"
django-version:
- "3.2"
- "4.0"
drf-version:
- "3.12"
- "3.13"
exclude:
- python-version: "3.7"
django-version: "4.0"
- drf-version: "3.12"
django-version: "4.0"

steps:
- uses: actions/checkout@v2

- name: Set up Python ${{ matrix.python-version }}
uses: actions/setup-python@v4
with:
python-version: ${{ matrix.python-version }}

- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install codecov black
- name: Lint with black
run: black ./drf_ip_restrictions ./tests --check

- name: Install Django version
run: |
python -m pip install "Django==${{ matrix.django-version }}.*"
- name: Install DRF version
run: |
python -m pip install "djangorestframework==${{ matrix.drf-version }}.*"
- name: Python, Django and DRF versions
run: |
echo "Python ${{ matrix.python-version }} -> Django ${{ matrix.django-version }} -> DRF ${{ matrix.drf-version }}"
python --version
echo "Django: `django-admin --version`"
echo "DRF: `pip show djangorestframework|grep Version|sed s/Version:\ //`"
- name: Setup environment
run: |
pip install -e .
- name: Run tests
run: |
# prepare Django project: link all necessary data from the test project into the root directory
# Hint: Simply changing the directory does not work (leads to missing files in coverage report)
ln -s ./tests/testapp testapp
ln -s ./tests/core core
ln -s ./tests/manage.py manage.py
# run tests with coverage
coverage run --source='./drf_ip_restrictions' manage.py test
coverage report
coverage xml
- name: Upload coverage to Codecov
uses: codecov/codecov-action@v2
15 changes: 15 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# Changelog
All notable changes to this project will be documented in this file.

The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [Unreleased]

## [1.0.0] - 2022-06-22
### Added
- Initial implementation
- Unit tests

[Unreleased]: https://github.com/anexia/drf-ip-restrictions/compare/1.0.0...HEAD
[1.0.0]: https://github.com/anexia/drf-ip-restrictions/releases/tag/1.0.0
35 changes: 35 additions & 0 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# Guidance on how to contribute

> By submitting a pull request or filing a bug, issue, or feature request,
> you are agreeing to comply with this waiver of copyright interest.
> Details can be found in our [LICENSE](LICENSE).

There are two primary ways to help:
- Using the issue tracker, and
- Changing the code-base.


## Using the issue tracker

Use the issue tracker to suggest feature requests, report bugs, and ask questions.
This is also a great way to connect with the developers of the project as well
as others who are interested in this solution.

Use the issue tracker to find ways to contribute. Find a bug or a feature, mention in
the issue that you will take on that effort, then follow the _Changing the code-base_
guidance below.


## Changing the code-base

Generally speaking, you should fork this repository, make changes in your
own fork, and then submit a pull request. All new code should have associated
unit tests that validate implemented features and the presence or lack of defects.
Additionally, the code should follow any stylistic and architectural guidelines
prescribed by the project. In the absence of such guidelines, mimic the styles
and patterns in the existing code-base.

### Contribution guidelines
- Your code should follow PEP 8 -- Style Guide for Python Code
- Your changes should be covered by unit-tests
29 changes: 22 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,18 +1,22 @@
# DRF IP Restrictions

[![PyPI version](https://img.shields.io/pypi/v/drf-ip-restrictions.svg)](https://pypi.org/project/drf-ip-restrictions/)
[![Run linter and tests](https://github.com/anexia/drf-ip-restrictions/actions/workflows/test.yml/badge.svg?branch=main)](https://github.com/anexia/drf-ip-restrictions/actions/workflows/test.yml)
[![Codecov](https://img.shields.io/codecov/c/gh/anexia/drf-ip-restrictions)](https://codecov.io/gh/anexia/drf-ip-restrictions)

A library that allows IP restrictions for views/endpoints in Django REST framework.

## Installation

1. Install using pip:

```
pip install git+https://github.com/anexia-it/drf-ip-restrictions@main
```sh
pip install drf-ip-restrictions
```

2. Add the library to your INSTALLED_APPS list.

```
```python
INSTALLED_APPS = [
...
'drf_ip_restrictions',
Expand All @@ -21,7 +25,8 @@ INSTALLED_APPS = [
```

4. Override the allowed IP addresses your `settings.py` according to your needs:
```

```python
# within settings.py

DRF_IP_RESTRICTION_SETTINGS = {
Expand All @@ -33,16 +38,18 @@ DRF_IP_RESTRICTION_SETTINGS = {

Add the AllowedIpList class to any views / endpoints that should only provide access for the
configured IP addresses, e.g. to restrict a view set:
```

```python
# within views.py

class MyViewSet(viewsets.ModelViewSet):
permission_classes = (AllowedIpList,)
...
```

or to restrict only a single endpoint:
```
or to restrict only a single action:

```python
# within views.py

class MyViewSet(viewsets.ModelViewSet):
Expand All @@ -59,3 +66,11 @@ class MyViewSet(viewsets.ModelViewSet):
def my_method(self, request, *args, **kwargs):
# do stuff and return rest_framework.response.Response in the end
```

## Django Compatibility Matrix

If your project uses an older verison of Django or Django Rest Framework, you can choose an older version of this project.

| This Project | Python Version | Django Version | Django Rest Framework |
|--------------|---------------------|----------------|-----------------------|
| 1.0.* | 3.7, 3.8, 3.9, 3.10 | 3.2, 4.0 | 3.12, 3.13 |
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
description='A library that allows IP restrictions for views/endpoints in Django REST framework.',
long_description=README,
long_description_content_type='text/markdown',
url='https://github.com/anexia-it/drf-ip-restrictions',
url='https://github.com/anexia/drf-ip-restrictions',
author='Alexandra Bruckner',
author_email='[email protected]',
install_requires=[
Expand Down
77 changes: 38 additions & 39 deletions tests/core/settings.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
"""
Django settings for "tests" project which assures the functionality of the "drf-ip-restrictions" package
(https://github.com/anexia-it/drf-ip-restrictions).
(https://github.com/anexia/drf-ip-restrictions).
"""

import os
Expand All @@ -9,7 +9,7 @@
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))

# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = 'H0&?S3ZyJVDoLdvzU6OhQ1i?rabJ7Pwd3#2NF.ge7NgWrRQKX$utvpd!4MPL'
SECRET_KEY = "H0&?S3ZyJVDoLdvzU6OhQ1i?rabJ7Pwd3#2NF.ge7NgWrRQKX$utvpd!4MPL"

# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True
Expand All @@ -19,55 +19,54 @@
# Application definition

INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',

'rest_framework',
'drf_ip_restrictions',
'testapp',
"django.contrib.admin",
"django.contrib.auth",
"django.contrib.contenttypes",
"django.contrib.sessions",
"django.contrib.messages",
"django.contrib.staticfiles",
"rest_framework",
"drf_ip_restrictions",
"testapp",
]

MIDDLEWARE = [
'django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
"django.middleware.security.SecurityMiddleware",
"django.contrib.sessions.middleware.SessionMiddleware",
"django.middleware.common.CommonMiddleware",
"django.middleware.csrf.CsrfViewMiddleware",
"django.contrib.auth.middleware.AuthenticationMiddleware",
"django.contrib.messages.middleware.MessageMiddleware",
"django.middleware.clickjacking.XFrameOptionsMiddleware",
]

ROOT_URLCONF = 'core.urls'
ROOT_URLCONF = "core.urls"

TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [],
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
'django.template.context_processors.debug',
'django.template.context_processors.request',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
"BACKEND": "django.template.backends.django.DjangoTemplates",
"DIRS": [],
"APP_DIRS": True,
"OPTIONS": {
"context_processors": [
"django.template.context_processors.debug",
"django.template.context_processors.request",
"django.contrib.auth.context_processors.auth",
"django.contrib.messages.context_processors.messages",
],
},
},
]

WSGI_APPLICATION = 'core.wsgi.application'
WSGI_APPLICATION = "core.wsgi.application"

# Database
# https://docs.djangoproject.com/en/3.2/ref/settings/#databases

DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
"default": {
"ENGINE": "django.db.backends.sqlite3",
"NAME": os.path.join(BASE_DIR, "db.sqlite3"),
}
}

Expand All @@ -76,25 +75,25 @@

AUTH_PASSWORD_VALIDATORS = [
{
'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator',
"NAME": "django.contrib.auth.password_validation.UserAttributeSimilarityValidator",
},
{
'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator',
"NAME": "django.contrib.auth.password_validation.MinimumLengthValidator",
},
{
'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator',
"NAME": "django.contrib.auth.password_validation.CommonPasswordValidator",
},
{
'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator',
"NAME": "django.contrib.auth.password_validation.NumericPasswordValidator",
},
]

# Internationalization
# https://docs.djangoproject.com/en/3.2/topics/i18n/

LANGUAGE_CODE = 'en-us'
LANGUAGE_CODE = "en-us"

TIME_ZONE = 'UTC'
TIME_ZONE = "UTC"

USE_I18N = True

Expand Down
Loading

0 comments on commit 1a73d6c

Please sign in to comment.