Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dev have to be possible without installing Product Opener #223

Open
CharlesNepote opened this issue Jan 17, 2025 · 1 comment
Open

Dev have to be possible without installing Product Opener #223

CharlesNepote opened this issue Jan 17, 2025 · 1 comment

Comments

@CharlesNepote
Copy link
Member

CharlesNepote commented Jan 17, 2025

It's not currently possible to test Folksonomy API locally without installing Product Opener, due to authentication concerns.

To test the API using http://api.folksonomy.openfoodfacts.localhost:8888/docs Folksonomy API need that the user is identified. And the authentication is done through Product Opener. Here is the code that rules this behavior:

In folksonomy/ap.py

def get_auth_server(request: Request):
    """
    Get auth server URL from request

    We deduce it by changing part of the request base URL
    according to FOLKSONOMY_PREFIX and AUTH_PREFIX settings
    """
    base_url =  f"{request.base_url.scheme}://{request.base_url.netloc}"
    # remove folksonomy prefix and add AUTH prefix
    base_url = base_url.replace(settings.FOLKSONOMY_PREFIX or "", settings.AUTH_PREFIX or "")
    return base_url

# Then get_auth_server is used to build the url used by Folksonomy to request authentication:
auth_url = get_auth_server(request) + "/cgi/auth.pl"

Following a config file:

# OpenFoodFacts username and password for authentication for tests only!
OFF_TEST_USER = 'myusername'
OFF_TEST_PASSWORD = 'mypassword'

# Postgres
POSTGRES_USER = '' # Leave empty if no user exists for database
POSTGRES_PASSWORD = '' # Leave empty if no password exists for user
POSTGRES_HOST = '' # Change if necessary
POSTGRES_DATABASE = 'folksonomy/'

# for dev using a local product opener instance you can use this
# base domain has to be the same (see INSTALL.md)
#FOLKSONOMY_PREFIX="api.folksonomy.openfoodfacts.localhost:8888"
#AUTH_PREFIX="world.openfoodfacts.localhost"

See also instruction in install documentation.

I don't understand why deduce the auth server from the current request. Why not puting the auth server in a config variable (eg. AUTH_SERVER_URL). This would allow to easily switch between cases:

  • testing Folksonomy API with Product Opener installed locally: AUTH_SERVER_URL="http://world.openfoodfacts.localhost"
  • testing Folksonomy API without Product Opener installed locally: AUTH_SERVER_URL="https://world.openfoodfacts.org"
  • production: AUTH_SERVER_URL="https://world.openfoodfacts.org"
@alexgarel
Copy link
Member

A simple solution is to add a AUTH_SERVER_STATIC variable in envs (or in local_settings) and if it exists and is not empty, use that instead of the base_url (still do it in get_auth_server)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Status: Todo
Development

No branches or pull requests

2 participants