Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ This repo contains the samples for [Keploy's](https://keploy.io) integration wit

4. [FastAPI-Twilio](https://github.com/keploy/samples-python/tree/main/fastapi-twilio) - This application is a SMS sending API built using Python's FastAPI and Twilio for their SMS sharing service.

5. [Flask-Redis](https://github.com/keploy/samples-python/tree/main/flask-redis) - This Flask-based application provides a book management system utilizing Redis for caching and storage. It supports adding, retrieving, updating, and deleting book records, with optimized search functionality and cache management for improved performance. The API endpoints ensure efficient data handling and quick access to book information.

## Community Support ❤️

### 🤔 Questions?
Expand Down
26 changes: 26 additions & 0 deletions flask-redis/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# Use the official Python image from the Docker Hub
FROM python:3.10-slim

# RUN apt-get update && apt-get install -y curl
# Set the working directory in the container
WORKDIR /app

# Copy the requirements file into the container
COPY requirements.txt .

# Install the Python dependencies
RUN pip install --no-cache-dir -r requirements.txt

# Copy the rest of the application code into the container
COPY . .

# Download Keploy CA certificate and setup script
# RUN curl -o ca.crt https://raw.githubusercontent.com/keploy/keploy/main/pkg/core/proxy/asset/ca.crt && \
# curl -o setup_ca.sh https://raw.githubusercontent.com/keploy/keploy/main/pkg/core/proxy/asset/setup_ca.sh && \
# chmod +x setup_ca.sh # Make the setup script executable

# Expose the port the app runs on
EXPOSE 5000

# Define the command to run the app
CMD ["python", "app.py"]
160 changes: 160 additions & 0 deletions flask-redis/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,160 @@
# flask-redis

A sample App using flask and redis

## Setup application

1. Clone the repository and move to flask-redis folder
2. Create a .env file and copy-paste below credentials:

```bash
REDIS_HOST=redis
REDIS_PORT=6379
```

# Installing Redis

```sh
brew install redis
```
If homebrew is not installed, then go to https://brew.sh/ and install it.

```bash
git clone https://github.com/keploy/samples-typescript && cd samples-typescript/flask-redis

# Install the dependencies
pip3 install -r requirements.txt
```

# Installing Keploy

Let's get started by setting up the Keploy alias with this command:

```sh
curl -O https://raw.githubusercontent.com/keploy/keploy/main/keploy.sh && source keploy.sh
```

## Using Keploy :

There are 2 ways you can run this sample application.

1. [Natively on Linux/WSL](#natively-on-ubuntuwsl)
2. [Using Docker](#running-sample-app-using-docker)

# Natively on Ubuntu/WSL

## Let's install certificates

1. **Install required packages:**

```sh
sudo apt-get install -y --no-install-recommends ca-certificates curl
```

This command installs necessary packages without additional recommended packages.

2. **Download CA certificate:**

```sh
curl -o ca.crt https://raw.githubusercontent.com/keploy/keploy/main/pkg/core/proxy/asset/ca.crt
```

This command downloads the CA certificate to `ca.crt`.

3. **Download setup script:**

```sh
curl -o setup_ca.sh https://raw.githubusercontent.com/keploy/keploy/main/pkg/core/proxy/asset/setup_ca.sh
```

This command downloads the setup script to `setup_ca.sh`.

4. **Make the setup script executable:**

```sh
chmod +x setup_ca.sh
```

This command changes the permissions of `setup_ca.sh` to make it executable.

5. **Run the setup script:**

```sh
source ./setup_ca.sh
```

This command executes the setup script in the current shell.

6. **Start the redis server:**
```sh
redis-server
```
This command starts the redis server.

## Capture the test cases

1. **Start recording tests:**
```bash
sudo -E env "PATH=$PATH" keploybin record -c 'python3 app.py'
```

## Let's Generate the test cases

Make API Calls using Hoppscotch, Postman or cURL command. Keploy will capture those calls to generate test suites containing test cases and data mocks.

1. Refer to flask-redis/api.txt to make api calls.

2. **Observe terminal output:**
Let's go ahead and create a few more test cases for different endpoints!

## Running the test cases

1. **Start the application:**

```bash
python3 app.py
```

2. **Run the recorded tests:**

```bash
sudo -E env "PATH=$PATH" keploybin test -c 'python3 app.py' --delay 10
```

3. **Observe test run results:**
_Voila!! Our test cases have passed 🌟_

---

# Using Docker

Since we have to setup our app using docker(make sure your docker is running)

## Create a custom network for Keploy since we are using the Docker

```bash
docker network create keploy-network
```

## Capture the testcases

We will run the keploy in record mode with docker-compose to start our application:-

```bash
keploy record -c "sudo docker-compose up" --containerName "flask-web"

```

#### Let's generate the testcases.

Make API Calls using [Hoppscotch](https://hoppscotch.io), [Postman](https://postman.com) or curl command. Keploy with capture those calls to generate the test-suites containing testcases and data mocks.

1. Refer to flask-redis/api.txt to make api calls

## Running the testcases

```bash
keploy test -c 'sudo docker-compose up' --containerName "flask-web" --delay 10
```

_Voila!! Our testcases has passed 🌟_
44 changes: 44 additions & 0 deletions flask-redis/api.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
---------------------------------------------------------------------------------------------------
// Add a Book

curl -X POST http://localhost:5000/books/ \
-H "Content-Type: application/json" \
-d '{"title": "1984", "author": "George Orwell"}'

---------------------------------------------------------------------------------------------------

// Get All Books(with Pagination)

curl -X GET "http://localhost:5000/books/?page=1&limit=10"

---------------------------------------------------------------------------------------------------

// Get Book Details

curl -X GET http://localhost:5000/books/1

---------------------------------------------------------------------------------------------------


//Search Books by Title

curl -X GET "http://localhost:5000/books/search?query=1984"


---------------------------------------------------------------------------------------------------

//Update a Book

curl -X PUT http://localhost:5000/books/1 \
-H "Content-Type: application/json" \
-d '{"title": "1984 - Updated", "author": "George Orwell"}'


---------------------------------------------------------------------------------------------------

//Delete a Book

curl -X DELETE http://localhost:5000/books/1

---------------------------------------------------------------------------------------------------

14 changes: 14 additions & 0 deletions flask-redis/app.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
from flask import Flask
from routes.book_routes import book

app = Flask(__name__)

# Register Blueprints
app.register_blueprint(book, url_prefix="/books")

@app.route('/', methods=['GET'])
def hello():
return {"message": "Welcome to the Book Management System!"}, 200

if __name__ == '__main__':
app.run(host='0.0.0.0', port=5000, debug=True)
36 changes: 36 additions & 0 deletions flask-redis/docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
version: "3.8"

services:
web:
build: .
container_name: flask-web
restart: always
ports:
- "5000:5000"
depends_on:
- redis
environment:
- FLASK_ENV=development
- REDIS_HOST=redis
- REDIS_PORT=6379
# volumes:
# - .:/usr/src/app
networks:
- keploy-network

redis:
image: "redis:alpine"
container_name: redis-server
restart: always
ports:
- "6379:6379"
# volumes:
# - db_data:/var/lib/redis
networks:
- keploy-network

networks:
keploy-network:
external: true
# volumes:
# db_data:
Binary file added flask-redis/dump.rdb
Binary file not shown.
43 changes: 43 additions & 0 deletions flask-redis/keploy.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
path: ""
appId: 0
appName: ""
command: sudo docker-compose up
port: 0
dnsPort: 26789
proxyPort: 16789
debug: false
disableTele: false
disableANSI: false
containerName: flask-web
networkName: ""
buildDelay: 30
test:
selectedTests: {}
globalNoise:
global: {}
test-sets: {}
delay: 5
apiTimeout: 5
skipCoverage: false
coverageReportPath: ""
ignoreOrdering: true
mongoPassword: default@123
language: ""
removeUnusedMocks: false
fallBackOnMiss: false
jacocoAgentPath: ""
basePath: ""
mocking: true
ignoredTests: {}
record:
filters: []
recordTimer: 0s
configPath: ""
bypassRules: []
generateGithubActions: false
keployContainer: keploy-v2
keployNetwork: keploy-network
cmdType: native
inCi: false

# Visit [https://keploy.io/docs/running-keploy/configuration-file/] to learn about using keploy through configration file.
Loading