Backend Tests

Backend tests are carried out with the objective of verifying the correct status of each API endpoints of the application. In addition, checks are made in the database to verify the consistency of the data and that it is generated where appropriate.
Below is a script that connects to Firestore by Firebase as a NoSQL database.

import sys
import pprint
import unittest
import requests
import firebase_admin
from faker import Faker
from firebase_admin import firestore
from firebase_admin import credentials

cred = credentials.Certificate(r"C:\AntonioRodriguezFarias\serviceAccountKey.json")
firebase_admin.initialize_app(cred)
db = firestore.client()

fake = Faker('es_ES')
s = requests.session()

with open("API_KEY.txt", "r", encoding="utf-8") as key:
    for api in key:
        api = api.strip()
        API_KEY = api


class TestExample(unittest.TestCase):
    def test_01_example(self):
        #
        #
        # POST -> /auth/signin
        # Obtain Bearer token - SignIn
        #

        r = s.post(
            'https://testing-api.antonio-rodriguez.com/auth/signin',
            headers={'Accept': 'application/json', 'Content-Type': 'application/json'},
            json={
                "email": "antonio-rodriguez-farias.com",
                "password": "T3st.1234"
            }
        )

        token = r.json()
        ACCESS_TOKEN = token.get('data')  # Bearer token en variable ACCESS_TOKEN

        #
        #
        #

        print('\nAntonio Rodriguez - Example with Firestore')
        r = s.post(
            'https://testing-api.antonio-rodriguez.com/endpoint',
            headers={'Accept': 'application/json', 'Content-Type': 'application/json', 'x-api-key': f'{API_KEY}',
                     'Authorization': f'Bearer {ACCESS_TOKEN}'},
            json={
                "field": True,
                "another": [
                    {
                        "fieldEx1": 1,
                        "fieldEx2": 1,
                        "email": "antonio@rodriguezfarias.com",
                        "fieldEx3": 1,
                        "fieldEx4": 1
                    }
                ]
            }
        )
        if r.status_code == 200:
            print("-------------------------------------------------")
            print(str(r.request) + "  " + str(r.status_code) + " " + r.reason + " in " + str(r.elapsed))
            print("-------------------------------------------------")
        else:
            print('[FAILED]  returned -> {}'.format(r.status_code))
            sys.exit()

        # Cleaning in Firestore
        pp = pprint.PrettyPrinter(indent=4)
        """
        verify = db.collection(u'example') \
            .document(u'SrGjQmHEMjPxiwCoUbsK') \
            .collection('rodriguez') \
            .document('2532607044f9f8143c86bb9c32a3c2a5c297312c4b902ce358ac874f') #-> antonio@rodriguezfarias.com
        """

        # Cleaning in collection example2
        # time.sleep(1)
        example2 = db.collection(u'rodriguez').document(u'SrGjQmHEMjPxiwCoUbsK')
        example2.update({
            'membersSent': {
                '2532607044f9f8143c86bb9c32a3c2a5c297312c4b902ce358ac874f': {
                    'fieldEX1': 1,
                    'fieldEX2': 0,
                    'fieldEX3': 0,
                    'fieldEX4': 2
                },
                '8232c193994ecdef1226d7c180eb9094e4bfa6cae73071c2508db3e0': {
                    'fieldEX1': 0,
                    'fieldEX2': 0,
                    'fieldEX3': 2,
                    'fieldEX4': 0
                }
            },
            'sent': {
                'fieldEX1': 1,
                'fieldEX2': 0,
                'fieldEX3': 2,
                'fieldEX4': 2
            }
        })

        # Cleaning in collection clients
        # time.sleep(0.5)
        example3 = db.collection(u'antonio') \
            .document(u'SrGjQmHEMjPxiwCoUbsK') \
            .collection('rodriguez') \
            .document('2532607044f9f8143c86bb9c32a3c2a5c297312c4b902ce358ac874f')

        example3.update({
            'field': {
                'sent': {
                    'fieldEX1': 1,
                    'fieldEX2': 0,
                    'fieldEX3': 0,
                    'fieldEX4': 2
                },
                'used': {
                    'fieldEX1': 1,
                    'fieldEX2': 0,
                    'fieldEX3': 0,
                    'fieldEX4': 2
                }
            }
        })

        # Clean client
        # time.sleep(0.5)
        client = db.collection(u'antonio').document(u'SrGjQmHEMjPxiwCoUbsK')
        client.update({
            "status.clientID": 5
        })

        # pp.pprint(verify.to_dict())