Skip to content

Instantly share code, notes, and snippets.

@cb109
Last active May 30, 2025 06:51
Show Gist options
  • Select an option

  • Save cb109/f480e6890b543d4a6614f8776a9b8a9a to your computer and use it in GitHub Desktop.

Select an option

Save cb109/f480e6890b543d4a6614f8776a9b8a9a to your computer and use it in GitHub Desktop.

Revisions

  1. cb109 revised this gist May 30, 2025. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion views.py
    Original file line number Diff line number Diff line change
    @@ -1,5 +1,5 @@
    @api_view(("GET",))
    @permission_classes((IsAuthenticated,))
    @permission_classes(())
    def e2e_env_lookup(request):
    if not is_testrun():
    raise PermissionDenied()
  2. cb109 revised this gist May 30, 2025. 2 changed files with 3 additions and 4 deletions.
    3 changes: 1 addition & 2 deletions urls.py
    Original file line number Diff line number Diff line change
    @@ -1,7 +1,6 @@
    ...

    # Enable this view only during test runs.
    if os.environ["IS_TEST_RUN"]:
    if is_testrun():
    urlpatterns.extend(
    [
    # This view takes an env_var GET parameter and returns its value from os.environ.
    4 changes: 2 additions & 2 deletions views.py
    Original file line number Diff line number Diff line change
    @@ -1,8 +1,8 @@
    @api_view(("GET",))
    @permission_classes((IsAuthenticated,))
    def e2e_env_lookup(request):
    if request.user.username != "e2e":
    raise PermissionDenied("Access allowed only for e2e User")
    if not is_testrun():
    raise PermissionDenied()

    env_var = request.GET["env_var"]
    value = os.environ.get(env_var, None)
  3. cb109 revised this gist May 28, 2025. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion conftest.py
    Original file line number Diff line number Diff line change
    @@ -6,5 +6,5 @@ def e2e_my_test(db):
    my_object = ...
    # Note: We may have to make this URL relative or replace its domain
    # with live_server.url to avoid running into same-origin constraints
    # in the browser.
    # in the browser. Though cypress v12 upwards is fine with that!
    os.environ["MY_DYNAMIC_URL"] = my_object.url
  4. cb109 revised this gist May 28, 2025. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions conftest.py
    Original file line number Diff line number Diff line change
    @@ -5,6 +5,6 @@ def e2e_my_test(db):
    """We assume this fixture is run before launching the cypress spec using the same name."""
    my_object = ...
    # Note: We may have to make this URL relative or replace its domain
    # with live_server.url to avoid running into same-origin constraints
    # in the browser.
    # with live_server.url to avoid running into same-origin constraints
    # in the browser.
    os.environ["MY_DYNAMIC_URL"] = my_object.url
  5. cb109 revised this gist May 28, 2025. 1 changed file with 3 additions and 0 deletions.
    3 changes: 3 additions & 0 deletions conftest.py
    Original file line number Diff line number Diff line change
    @@ -4,4 +4,7 @@
    def e2e_my_test(db):
    """We assume this fixture is run before launching the cypress spec using the same name."""
    my_object = ...
    # Note: We may have to make this URL relative or replace its domain
    # with live_server.url to avoid running into same-origin constraints
    # in the browser.
    os.environ["MY_DYNAMIC_URL"] = my_object.url
  6. cb109 revised this gist May 28, 2025. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion urls.py
    Original file line number Diff line number Diff line change
    @@ -1,6 +1,6 @@
    ...

    # Enable this lookup only during test runs.
    # Enable this view only during test runs.
    if os.environ["IS_TEST_RUN"]:
    urlpatterns.extend(
    [
  7. cb109 revised this gist May 28, 2025. 1 changed file with 1 addition and 0 deletions.
    1 change: 1 addition & 0 deletions conftest.py
    Original file line number Diff line number Diff line change
    @@ -2,5 +2,6 @@


    def e2e_my_test(db):
    """We assume this fixture is run before launching the cypress spec using the same name."""
    my_object = ...
    os.environ["MY_DYNAMIC_URL"] = my_object.url
  8. cb109 revised this gist May 28, 2025. 1 changed file with 9 additions and 0 deletions.
    9 changes: 9 additions & 0 deletions views.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,9 @@
    @api_view(("GET",))
    @permission_classes((IsAuthenticated,))
    def e2e_env_lookup(request):
    if request.user.username != "e2e":
    raise PermissionDenied("Access allowed only for e2e User")

    env_var = request.GET["env_var"]
    value = os.environ.get(env_var, None)
    return Response(value)
  9. cb109 revised this gist May 28, 2025. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion urls.py
    Original file line number Diff line number Diff line change
    @@ -5,6 +5,6 @@
    urlpatterns.extend(
    [
    # This view takes an env_var GET parameter and returns its value from os.environ.
    re_path(r"^e2e/env$", client_views.e2e_env_lookup),
    re_path(r"^e2e/env$", e2e_env_lookup),
    ]
    )
  10. cb109 created this gist May 28, 2025.
    6 changes: 6 additions & 0 deletions conftest.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,6 @@
    import os


    def e2e_my_test(db):
    my_object = ...
    os.environ["MY_DYNAMIC_URL"] = my_object.url
    15 changes: 15 additions & 0 deletions e2e_my_test.cy.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,15 @@
    describe('Test against a dynamic URL', () => {
    it('should visit the dynamic URL', () => {

    const envLookupURL = Cypress.env('apiBaseUrl') + 'e2e/env' + '?env_var=MY_DYNAMIC_URL';

    cy.request({
    method: 'GET',
    url: envLookupURL,
    })
    .then((response) => {
    const dynamicURL = response.body;
    cy.visit(dynamicURL);
    });
    });
    });
    10 changes: 10 additions & 0 deletions urls.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,10 @@
    ...

    # Enable this lookup only during test runs.
    if os.environ["IS_TEST_RUN"]:
    urlpatterns.extend(
    [
    # This view takes an env_var GET parameter and returns its value from os.environ.
    re_path(r"^e2e/env$", client_views.e2e_env_lookup),
    ]
    )