Skip to content

Commit

Permalink
ci(tests/private-packages): Add SAML, auth controller and workflow (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
gagantrivedi authored Jul 1, 2024
1 parent 540d320 commit c2a33e6
Show file tree
Hide file tree
Showing 10 changed files with 80 additions and 58 deletions.
32 changes: 11 additions & 21 deletions .github/workflows/api-tests-with-private-packages.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,6 @@ defaults:
run:
working-directory: api

env:
FLAGSMITH_RBAC_REVISION: v0.7.0

jobs:
test:
if: ${{ github.event.label.name == 'api' }}
Expand Down Expand Up @@ -40,26 +37,19 @@ jobs:
python-version: '3.12'
cache: 'poetry'

- name: Install Dependencies
if: steps.cached-poetry-dependencies.outputs.cache-hit != 'true'
run: make install-packages

- name: Checkout rbac package
uses: actions/checkout@v4
with:
repository: flagsmith/flagsmith-rbac
token: ${{ secrets.GH_PRIVATE_ACCESS_TOKEN }}
ref: ${{ env.FLAGSMITH_RBAC_REVISION }}
path: ./flagsmith-rbac

- name: Integrate RBAC module
run: mv $GITHUB_WORKSPACE/flagsmith-rbac/rbac .
shell: bash
- name: Install SAML Dependencies
run: sudo apt-get install -y xmlsec1

- name: integrate rbac tests
run: ln -s $GITHUB_WORKSPACE/flagsmith-rbac/tests tests/rbac_tests
- name: Install packages and Tests
if: steps.cached-poetry-dependencies.outputs.cache-hit != 'true'
shell: bash
working-directory: api
run: |
echo "https://${{ secrets.GH_PRIVATE_ACCESS_TOKEN }}:@github.com" > ${HOME}/.git-credentials
git config --global credential.helper store
make install-packages opts="--with saml,auth-controller,workflows"
make install-private-modules
make integrate-private-tests
rm -rf ${HOME}/.git-credentials
- name: Run Tests
env:
Expand Down
10 changes: 10 additions & 0 deletions api/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -114,3 +114,13 @@ generate-ld-client-types:
--use-standard-collections \
--wrap-string-literal \
--special-field-name-prefix=

.PHONY: integrate-private-tests
integrate-private-tests:
$(eval WORKFLOW_REVISION := $(shell grep -A 1 "\[tool.poetry.group.workflows.dependencies\]" pyproject.toml | awk -F '"' '{printf $$4}'))
$(eval AUTH_CONTROLLER_REVISION := $(shell grep -A 1 "\[tool.poetry.group.auth-controller.dependencies\]" pyproject.toml | awk -F '"' '{printf $$4}'))
git clone https://github.com/flagsmith/flagsmith-saml --depth 1 --branch ${SAML_REVISION} && mv ./flagsmith-saml/tests tests/saml_tests
git clone https://github.com/flagsmith/flagsmith-rbac --depth 1 --branch ${RBAC_REVISION} && mv ./flagsmith-rbac/tests tests/rbac_tests
git clone https://github.com/flagsmith/flagsmith-workflows --depth 1 --branch ${WORKFLOW_REVISION} && mv ./flagsmith-workflows/tests tests/workflow_tests
git clone https://github.com/flagsmith/flagsmith-auth-controller --depth 1 --branch ${AUTH_CONTROLLER_REVISION} && mv ./flagsmith-auth-controller/tests tests/auth_controller_tests
rm -rf ./flagsmith-saml ./flagsmith-rbac ./flagsmith-workflows ./flagsmith-auth-controller
27 changes: 8 additions & 19 deletions api/poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion api/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ flagsmith-auth-controller = { git = "https://github.com/flagsmith/flagsmith-auth
optional = true

[tool.poetry.group.saml.dependencies]
pysaml2 = "^7.0.0"
pysaml2 = "^7.4.2"

[tool.poetry.group.ldap]
optional = true
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from django.conf import settings
from django.core import mail
from django.urls import reverse
from pytest_mock import MockerFixture
from rest_framework import status
from rest_framework.test import APIClient, override_settings

Expand Down Expand Up @@ -288,12 +289,14 @@ def test_throttle_login_workflows(
api_client: APIClient,
db: None,
reset_cache: None,
mocker: MockerFixture,
) -> None:
# verify that a throttle rate exists already then set it
# to something easier to reliably test
assert settings.REST_FRAMEWORK["DEFAULT_THROTTLE_RATES"]["login"]
settings.REST_FRAMEWORK["DEFAULT_THROTTLE_RATES"]["login"] = "1/sec"

mocker.patch(
"rest_framework.throttling.ScopedRateThrottle.get_rate", return_value="1/minute"
)
email = "test@example.com"
password = FFAdminUser.objects.make_random_password()
register_data = {
Expand Down Expand Up @@ -323,11 +326,19 @@ def test_throttle_login_workflows(
assert login_response.status_code == status.HTTP_429_TOO_MANY_REQUESTS


def test_throttle_signup(api_client, settings, user_password, db, reset_cache):
def test_throttle_signup(
api_client: APIClient,
user_password: str,
db: None,
reset_cache: None,
mocker: MockerFixture,
) -> None:
# verify that a throttle rate exists already then set it
# to something easier to reliably test
assert settings.REST_FRAMEWORK["DEFAULT_THROTTLE_RATES"]["signup"]
settings.REST_FRAMEWORK["DEFAULT_THROTTLE_RATES"]["signup"] = "1/min"
mocker.patch(
"rest_framework.throttling.ScopedRateThrottle.get_rate", return_value="1/minute"
)
# Next, let's hit signup for the first time
register_data = {
"email": "user_1_email@mail.com",
Expand All @@ -351,9 +362,13 @@ def test_throttle_signup(api_client, settings, user_password, db, reset_cache):
assert response.status_code == status.HTTP_429_TOO_MANY_REQUESTS


def test_get_user_is_not_throttled(admin_client, settings, reset_cache):
def test_get_user_is_not_throttled(
admin_client: APIClient, reset_cache: None, mocker: MockerFixture
):
# Given
settings.REST_FRAMEWORK["DEFAULT_THROTTLE_RATES"]["signup"] = "1/min"
mocker.patch(
"rest_framework.throttling.ScopedRateThrottle.get_rate", return_value="1/minute"
)
url = reverse("api-v1:custom_auth:ffadminuser-me")
# When
for _ in range(2):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ def test_create_oauth_login_serializer(
sign_up_type = "NO_INVITE"
data = {"access_token": access_token, "sign_up_type": sign_up_type}
rf = RequestFactory()
request = rf.post("placeholer-login-url")
request = rf.post("/api/v1/auth/oauth/google/")
email = "testytester@example.com"
first_name = "testy"
last_name = "tester"
Expand Down
21 changes: 16 additions & 5 deletions api/tests/unit/custom_auth/test_unit_custom_auth_serializer.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,30 @@
}


def test_CustomUserCreateSerializer_converts_email_to_lower_case(db):
def test_CustomUserCreateSerializer_converts_email_to_lower_case(
db: None, rf: RequestFactory
) -> None:
# Given
serializer = CustomUserCreateSerializer(data=user_dict)
request = rf.post("/api/v1/auth/users/")
serializer = CustomUserCreateSerializer(
data=user_dict, context={"request": request}
)
# When
serializer.is_valid(raise_exception=True)
# Then
assert serializer.validated_data["email"] == "testuser@mail.com"


def test_CustomUserCreateSerializer_does_case_insensitive_lookup_with_email(db):
def test_CustomUserCreateSerializer_does_case_insensitive_lookup_with_email(
db: None, rf: RequestFactory
) -> None:
# Given
request = rf.post("/api/v1/auth/users/")
FFAdminUser.objects.create(email="testuser@mail.com")
serializer = CustomUserCreateSerializer(data=user_dict)

serializer = CustomUserCreateSerializer(
data=user_dict, context={"request": request}
)

# When
assert serializer.is_valid() is False
Expand Down Expand Up @@ -72,7 +83,7 @@ def test_CustomUserCreateSerializer_allows_registration_if_sign_up_type_is_invit
}

serializer = CustomUserCreateSerializer(
data=data, context={"request": rf.post("/v1/auth/users/")}
data=data, context={"request": rf.post("/api/v1/auth/users/")}
)
assert serializer.is_valid()

Expand Down
Loading

0 comments on commit c2a33e6

Please sign in to comment.