Reputation: 13
I'm trying to run unit tests on a Flask application that utilizes public and private keys for JWT token generation. However, when running my unit tests, I'm getting a FileNotFoundError indicating that the private key file cannot be found.
Here's the structure of my project:
project/
│
├── app/
│ ├── __init__.py
│ ├── user.py
│ ├── news.py
│ ├── public_key.pem
│ └── private_key.pem
│
└── tests/
├── __init__.py
└── test_app.py
The private_key.pem file is used to sign the JWT tokens in the Flask application. However, when running my unit tests, I receive the following error:
FileNotFoundError: [Errno 2] No such file or directory: 'private_key.pem'
Here's a snippet of the code where the private key is loaded:
# Load the private key
with open('private_key.pem', 'r') as f:
private_key = f.read()
my terminal:
PS C:\Users\ferre\Projects\Portal-de-Noticias> & c:/Users/ferre/Projects/Portal-de-Noticias/.venv/Scripts/python.exe c:/Users/ferre/Projects/Portal-de-Noticias/backend/user_registration/test_app.py
Arquivo public_key.pem não encontrado.
Traceback (most recent call last):
File "c:\Users\ferre\Projects\Portal-de-Noticias\backend\user_registration\test_app.py", line 4, in <module>
from user import user_bp
File "c:\Users\ferre\Projects\Portal-de-Noticias\backend\user_registration\user.py", line 54, in <module>
with open('private_key.pem', 'r') as f:
^^^^^^^^^^^^^^^^^^^^^^^^^^^^
FileNotFoundError: [Errno 2] No such file or directory: 'private_key.pem'
PS C:\Users\ferre\Projects\Portal-de-Noticias>
my test code:
import unittest
from flask import Flask
from flask_cors import CORS
from user import user_bp
from noticias import noticias_bp
class TestApp(unittest.TestCase):
def setUp(self):
# Configurar a aplicação Flask para testes
self.app = Flask(__name__)
CORS(self.app)
self.app.secret_key = '2002'
self.app.register_blueprint(user_bp)
self.app.register_blueprint(noticias_bp)
# Configurar o cliente de teste
self.client = self.app.test_client()
def test_user_blueprint_registration(self):
# Verificar se o blueprint do usuário está registrado na aplicação Flask
self.assertIn('user', self.app.blueprints)
def test_noticias_blueprint_registration(self):
# Verificar se o blueprint de notícias está registrado na aplicação Flask
self.assertIn('noticias', self.app.blueprints)
def test_user_blueprint_routes(self):
# Testar se as rotas do blueprint do usuário estão acessíveis
response = self.client.get('/user/')
self.assertEqual(response.status_code, 200)
response = self.client.get('/user/profile')
self.assertEqual(response.status_code, 200)
def test_noticias_blueprint_routes(self):
# Testar se as rotas do blueprint de notícias estão acessíveis
response = self.client.get('/noticias/')
self.assertEqual(response.status_code, 200)
response = self.client.get('/noticias/latest')
self.assertEqual(response.status_code, 200)
if __name__ == '__main__':
unittest.main()
How can I solve this issue and get my unit tests to work correctly? Should I include the private key directly in the test code, or is there a better approach to handling this?
Thanks in advance for any help!
Upvotes: -1
Views: 34
Reputation: 120
When you run the code from the shell you are still in working directory C:\Users\ferre\Projects\Portal-de-Noticias
but the keys are in the C:\Users\ferre\Projects\Portal-de-Noticias\app
so you should not be able to access them by basename as ""private_key.pem folder.
Try to use instead app/private_key.pem
or navigate to app before tests run
Upvotes: 0