Skip to content
This repository has been archived by the owner on Jul 12, 2024. It is now read-only.

Commit

Permalink
feat: api key validation feature
Browse files Browse the repository at this point in the history
Co-authored-by: loyal812 <jh.chan0812@gmail.com>
  • Loading branch information
eureka320 and loyal812 committed Mar 27, 2024
1 parent a98b4fb commit c3439d8
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 1 deletion.
56 changes: 56 additions & 0 deletions check_api_key.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@

import os
import gc
import argparse
from pathlib import Path

from src.utils.read_json import read_json
from src.mongodb.MongoDBClass import MongoDBClass
from pathlib import Path

def delete_api_key(args):
"""
main entry point
"""

# Payload
payload_data = read_json(args.payload_dir)

# Call class instance
mongodb = MongoDBClass(
db_name=payload_data["db_name"],
collection_name=payload_data["collection_name"],
mongo_uri=payload_data["mongo_uri"])

mongodb.check_validation_api(api_key=str(Path(args.api_key)), user=str(Path(args.user)))

gc.collect()

if __name__ == "__main__":
"""
Form command lines
"""
# Clean up buffer memory
gc.collect()

# Current directory
current_dir = os.path.dirname(os.path.abspath(__file__))

# Payload directory
test_name = "regression_test013"
payload_name = "mongodb_payload.json"
payload_dir = os.path.join(current_dir, "test", "regression", test_name, "payload", payload_name)

user = "user@gmail.com"
api_key = "AMEYbpdcmrUxNu_Fb80qutukUZdlsmYiH4g7As5LzNA"
description = "description"

# Add options
p = argparse.ArgumentParser()
p = argparse.ArgumentParser(description="Translate text within an image.")
p.add_argument("--payload_dir", type=Path, default=payload_dir, help="payload directory to the test example")
p.add_argument("--user", type=Path, default=user, help="user")
p.add_argument("--api_key", type=Path, default=api_key, help="title")
args = p.parse_args()

delete_api_key(args)
22 changes: 21 additions & 1 deletion src/mongodb/MongoDBClass.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,4 +117,24 @@ def delete_api(self, api_key, user):
print("No matching document found")
else:
return db


def check_validation_api(self, api_key, user):
# Connect to MongoDB
db = self.mongo_connect()

if db["result"] == True:
collection = db['message']

# Define the filter condition to check for document existence
filter_condition = {"api": api_key, "user": user, "is_removed": False}

# Check if a document exists that matches the filter condition
existing_document = collection.find_one(filter_condition)
if existing_document:
print("Document exists in the collection")
return True
else:
print("Document does not exist in the collection")
return False
else:
return db

0 comments on commit c3439d8

Please sign in to comment.