Skip to content
41 changes: 40 additions & 1 deletion besecure_developer_toolkit/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
from besecure_developer_toolkit.src.create_ossp_master import OSSPMaster
from besecure_developer_toolkit.src.create_version_data import Version
from besecure_developer_toolkit.src.generate_report import Report
from besecure_developer_toolkit.src.vdnc import VersionFileValidate
import ssl
ssl._create_default_https_context = ssl._create_stdlib_context

Expand Down Expand Up @@ -81,8 +82,12 @@ def set_env_vars():

app = typer.Typer()
generate_app = typer.Typer()
app.add_typer(generate_app, name="generate")
validate = typer.Typer()

app.add_typer(generate_app, name="generate")
app.add_typer(validate,
name="validate",
help="Version details file naming convention validate")

def _version_callback(value: bool) -> None:
if value:
Expand Down Expand Up @@ -149,6 +154,40 @@ def report(
raise typer.Exit()


@validate.command("version_file")
def version_data_naming_convention_validation(
issue_id: int = typer.Option(
None,
prompt="Enter OSSP id",
help="OSSP id"
),
name: str = typer.Option(
None,
prompt="Enter OSSP name",
help="OSSP name"
),
namespace: str = typer.Option(
None,
prompt="Enter GitHub username",
help="GitHub Username"
),
branch: str = typer.Option(
None,
prompt="Enter branch",
help="besecure-osspoi-datastore branch"
),
):
""" Check version details file naming convention """
version_data = VersionFileValidate(issue_id, name, namespace, branch)
version_data.verify_versiondetails_name()


@validate.command("report_file")
def report_naming_convention_validation():
""" Check report file naming convention """
print("Under Development")


@app.callback()
def main(
version: Optional[bool] = typer.Option(
Expand Down
70 changes: 70 additions & 0 deletions besecure_developer_toolkit/src/vdnc.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
"""Version details file validate"""
import sys
from urllib.error import HTTPError
from urllib.request import urlopen
from rich import print
from besecure_developer_toolkit.src.create_ossp_master import OSSPMaster


class VersionFileValidate():
"""version details file naming convention validation class"""

def __init__(self, issue_id: int, name: str, namespace: str, branch: str):
self.issue_id = issue_id
self.name = name
self.namespace = namespace
self.branch = branch

def check_username(self):
"""This method checks if given username is valid or not"""
try:
urlopen("https://api.github.com/users/"+self.namespace)
except HTTPError:
print(f"[bold red]Alert! [green]"
f"{self.namespace} is not valid username")
sys.exit()

def check_branch_exists(self):
"""This method checks if the branch is exists
under besecure-osspoi-datastore for given user"""
try:
urlopen("https://api.github.com/repos/"+self.namespace +
"/besecure-osspoi-datastore/branches/"+self.branch)
except HTTPError:
print(f"[bold red]Alert! [green]{self.branch} does not "
f"exists under besecure-osspoi-"
f"datastore for {self.namespace}")
sys.exit()

def check_repo_exists(self, flag, namespace) -> None:
"""This is an overriding method of CreateOsspMaster class
It checks if besecure-osspoi-datastore repo exists under the given user,
here flag is used for differentiate between base & child class method"""
try:
if flag:
urlopen("https://api.github.com/repos/" +
namespace+"/besecure-osspoi-datastore")
except HTTPError:
print(f"[bold red]Alert! [green]Could not find "
f"besecure-osspoi-datastore under {namespace}")
sys.exit()

def verify_versiondetails_name(self):
"""It checks the version details file naming
convention that is uploaded by user"""
obj = OSSPMaster(self.issue_id, self.name)
obj.check_issue_exists(self.issue_id)
obj.check_repo_exists(self.name)
obj.check_issue_related_to_project()
self.check_username()
self.check_repo_exists(True, self.namespace)
self.check_branch_exists()
try:
urlopen("https://raw.githubusercontent.com/"
+ self.namespace+"/besecure-osspoi-datastore/"
+ self.branch+"/version_details/"+str(self.issue_id)
+ "-"+self.name+"-Versiondetails.json")
print(f"{self.issue_id}-{self.name}-Versiondetails.json exists")
except HTTPError:
print(f"[bold red]Alert! [green]{self.issue_id}-{self.name}-"
f"Versiondetails.json does not exists")
161 changes: 161 additions & 0 deletions tests/test_vdnc.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,161 @@
"""Test cases for version details file naming convention"""
from typer.testing import CliRunner
from besecure_developer_toolkit.cli import app
runner = CliRunner()


def test_vdnc():
"""test with valid data"""
issue_id = 136
name = "fastjson"
namespace = "Be-Secure"
branch = "main"

result = runner.invoke(app, ["validate", "version_file"], input=str(issue_id) +
"\n"+name+"\n"+namespace+"\n"+branch+"\n")
result = result.output
result = result.split("branch")[1]
result = result.strip("\n")[1:]
assert result == "136-fastjson-Versiondetails.json exists"


def test_vdnc_file_does_not_exists():
"""test with wrong/invalid file name"""
issue_id = 405
name = "OpenIDM"
namespace = "pramit-d"
branch = "main"

result = runner.invoke(app, ["validate", "version_file"], input=str(issue_id) +
"\n"+name+"\n"+namespace+"\n"+branch+"\n")
result = result.output
result = result.split("branch")[1]
result = result.strip("\n")[1:]
assert result == "Alert! 405-OpenIDM-"\
"Versiondetails.json does not exists"


def test_vdnc_with_invalid_id():
"""test with invlid id"""
issue_id = 500
name = "Koha"
namespace = "pramit-d"
branch = "Koha"

result = runner.invoke(app, ["validate", "version_file"], input=str(issue_id) +
"\n"+name+"\n"+namespace+"\n"+branch+"\n")
result = result.output
result = result.split("branch")[1]
result = result.strip("\n")[1:]
assert result == "Could not find issue with id : 500"


def test_vdnc_with_invalid_version_tag():
"""check version tag present under besecure issue details &
the tag written inside the version details file is not same"""
issue_id = 148
name = "Koha"
namespace = "pramit-d"
branch = "Koha"

result = runner.invoke(app, ["validate", "version_file"], input=str(
issue_id)+"\n"+name+"\n"+namespace+"\n"+branch+"\n")
result = result.output
result = result.split("branch")[1]
result = result.strip("\n")[1:]
assert result == "Alert! Mismatch Version tag : Issue- "\
"v21.05.18 & Versiondetails file- v21.05.17"


def test_vdnc_with_invalid_username():
"""test with invalid username"""
issue_id = 136
name = "fastjson"
namespace = "prdut"
branch = "main"

result = runner.invoke(app, ["validate", "version_file"], input=str(issue_id) +
"\n"+name+"\n"+namespace+"\n"+branch+"\n")
result = result.output
result = result.split("branch")[1]
result = result.strip("\n")[1:]
assert result == "Alert! prdut is not valid username"


def test_vdnc_with_invalid_branch():
"""test with invalid branch name"""
issue_id = 197
name = "bit"
namespace = "pramit-d"
branch = "xyz"

result = runner.invoke(app, ["validate", "version_file"], input=str(issue_id) +
"\n"+name+"\n"+namespace+"\n"+branch+"\n")
result = result.output
result = result.split("branch")[1]
result = result.strip("\n")[1:]
assert result == "Alert! xyz does not "\
"exists under besecure-osspoi-datastore for pramit-d"


def test_vdnc_with_invalid_repo():
"""besecure-osspoi-datastore does not exists under mentioned user"""
issue_id = 197
name = "bit"
namespace = "arunpillai6"
branch = "main"

result = runner.invoke(app, ["validate", "version_file"], input=str(issue_id) +
"\n"+name+"\n"+namespace+"\n"+branch+"\n")
result = result.output
result = result.split("branch")[1]
result = result.strip("\n")[1:]
assert result == "Alert! Could not find "\
"besecure-osspoi-datastore under arunpillai6"


def test_vdnc_with_wrong_project():
"""besecure-osspoi-datastore does not exists under mentioned user"""
issue_id = 197
name = "Koha"
namespace = "Be-Secure"
branch = "main"

result = runner.invoke(app, ["validate", "version_file"], input=str(issue_id) +
"\n"+name+"\n"+namespace+"\n"+branch+"\n")
result = result.output
result = result.split("branch")[1]
result = result.strip("\n").replace("\n", "")[1:]
assert result == "Alert! Mismatch issue_id-project : "\
"Issue id 197 does not match the project Koha"


def test_vdnc_with_invalid_project():
"""project does not exists under Be-Secure"""
issue_id = 197
name = "bitttt"
namespace = "Be-Secure"
branch = "main"

result = runner.invoke(app, ["validate", "version_file"], input=str(issue_id) +
"\n"+name+"\n"+namespace+"\n"+branch+"\n")
result = result.output
result = result.split("branch")[1]
result = result.strip("\n")[1:]
assert result == "Could not find bitttt under Be-Secure"


def test_vdnc_check_under_OSSPMaster():
"""check if mentioned project exists under OSSP-master.json file"""
issue_id = 405
name = "OpenIDM"
namespace = "pramit-d"
branch = "test-dev-kit"

result = runner.invoke(app, ["validate", "version_file"], input=str(issue_id) +
"\n"+name+"\n"+namespace+"\n"+branch+"\n")
result = result.output
result = result.split("branch")[1]
result = result.strip("\n")[1:]
assert result == "Alert! OpenIDM does not exists "\
"under besecure-osspoi-datastore"