Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update URLs, Lint #540

Merged
merged 1 commit into from
Jun 28, 2024
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
17 changes: 13 additions & 4 deletions safety/errors.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,16 @@
from typing import Optional
from safety.constants import EXIT_CODE_EMAIL_NOT_VERIFIED, EXIT_CODE_FAILURE, EXIT_CODE_INVALID_API_KEY, EXIT_CODE_TOO_MANY_REQUESTS, \
EXIT_CODE_UNABLE_TO_FETCH_VULNERABILITY_DB, EXIT_CODE_UNABLE_TO_LOAD_LOCAL_VULNERABILITY_DB, EXIT_CODE_MALFORMED_DB, \
EXIT_CODE_INVALID_PROVIDED_REPORT, EXIT_CODE_INVALID_REQUIREMENT

from safety.constants import (
EXIT_CODE_EMAIL_NOT_VERIFIED,
EXIT_CODE_FAILURE,
EXIT_CODE_INVALID_API_KEY,
EXIT_CODE_INVALID_PROVIDED_REPORT,
EXIT_CODE_INVALID_REQUIREMENT,
EXIT_CODE_MALFORMED_DB,
EXIT_CODE_TOO_MANY_REQUESTS,
EXIT_CODE_UNABLE_TO_FETCH_VULNERABILITY_DB,
EXIT_CODE_UNABLE_TO_LOAD_LOCAL_VULNERABILITY_DB,
)


class SafetyException(Exception):
Expand Down Expand Up @@ -83,7 +92,7 @@ class InvalidCredentialError(DatabaseFetchError):

def __init__(self, credential: Optional[str] = None, message="Your authentication credential{credential}is invalid. See {link}.", reason=None):
self.credential = credential
self.link = 'https://bit.ly/3OY2wEI'
self.link = 'https://docs.safetycli.com/safety-docs/support/invalid-api-key-error'
self.message = message.format(credential=f" '{self.credential}' ", link=self.link) if self.credential else message.format(credential=' ', link=self.link)
info = f" Reason: {reason}"
self.message = self.message + (info if reason else "")
Expand Down
16 changes: 8 additions & 8 deletions tests/test_cli.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
import json
import os
from pathlib import Path
import shutil
import tempfile
import unittest
from datetime import datetime
from packaging.version import Version
from packaging.specifiers import SpecifierSet
from unittest.mock import patch, Mock
from pathlib import Path
from unittest.mock import Mock, patch

import click
from click.testing import CliRunner
from packaging.specifiers import SpecifierSet
from packaging.version import Version

from safety import cli
from safety.models import Vulnerability, CVE, Severity, SafetyRequirement
from safety.models import CVE, SafetyRequirement, Severity, Vulnerability
from safety.util import Package, SafetyContext


Expand Down Expand Up @@ -70,7 +70,7 @@ def setUp(self):
self.output_options = ['screen', 'text', 'json', 'bare']
self.dirname = os.path.dirname(__file__)

def test_command_line_interface(self):
def test_command_line_interface(self):
runner = CliRunner()
result = runner.invoke(cli.cli)
expected = "Usage: cli [OPTIONS] COMMAND [ARGS]..."
Expand Down Expand Up @@ -234,7 +234,7 @@ def test_validate_with_basic_policy_file(self):
"cvss_severity": [
"critical",
"high",
"medium",
"medium",
],
"exploitability": [
"critical",
Expand Down Expand Up @@ -330,7 +330,7 @@ def test_check_with_fix_does_verify_api_key(self):
req_file = os.path.join(dirname, "test_fix", "basic", "reqs_simple.txt")
result = self.runner.invoke(cli.cli, ['check', '-r', req_file, '--apply-security-updates'])
self.assertEqual(click.unstyle(result.stderr),
"The --apply-security-updates option needs authentication. See https://bit.ly/3OY2wEI.\n")
"The --apply-security-updates option needs authentication. See https://docs.safetycli.com/safety-docs/support/invalid-api-key-error.\n")
self.assertEqual(result.exit_code, 65)

def test_check_with_fix_only_works_with_files(self):
Expand Down
38 changes: 27 additions & 11 deletions tests/test_safety.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,19 +19,35 @@
from unittest.mock import Mock, patch

import click as click
from packaging.version import parse
from packaging.specifiers import SpecifierSet
from packaging.version import parse
from requests.exceptions import RequestException

from safety.auth import build_client_session
from safety.constants import DB_CACHE_FILE

from safety.errors import DatabaseFetchError, DatabaseFileNotFoundError, MalformedDatabase, InvalidCredentialError, TooManyRequestsError
from safety.errors import (
DatabaseFetchError,
DatabaseFileNotFoundError,
InvalidCredentialError,
MalformedDatabase,
TooManyRequestsError,
)
from safety.formatter import SafetyFormatter
from safety.models import CVE, Package, SafetyRequirement
from safety.safety import get_announcements, ignore_vuln_if_needed, get_closest_ver, precompute_remediations, compute_sec_ver, \
calculate_remediations, read_vulnerabilities, check, get_licenses, review
from safety.safety import (
calculate_remediations,
check,
compute_sec_ver,
get_announcements,
get_closest_ver,
get_licenses,
ignore_vuln_if_needed,
precompute_remediations,
read_vulnerabilities,
review,
)
from safety.util import get_packages_licenses, read_requirements
from tests.resources import VALID_REPORT, VULNS, SCANNED_PACKAGES, REMEDIATIONS
from tests.resources import REMEDIATIONS, SCANNED_PACKAGES, VALID_REPORT, VULNS
from tests.test_cli import get_vulnerability


Expand Down Expand Up @@ -140,7 +156,7 @@ def test_check_live(self):
packages = read_requirements(reqs)

vulns, _ = check(
session=self.session,
session=self.session,
packages=packages,
db_mirror=False,
cached=0,
Expand Down Expand Up @@ -237,7 +253,7 @@ def test_get_packages_licenses_without_api_key(self):
telemetry=False
)
db_generic_exception = error.exception
self.assertEqual(str(db_generic_exception), 'Your authentication credential is invalid. See https://bit.ly/3OY2wEI.')
self.assertEqual(str(db_generic_exception), 'Your authentication credential is invalid. See https://docs.safetycli.com/safety-docs/support/invalid-api-key-error.')


def test_get_packages_licenses_with_invalid_api_key(self):
Expand Down Expand Up @@ -459,7 +475,7 @@ def test_get_announcements_catch_unhandled_http_codes(self, get_used_options):
api_key = "somekey"
session.api_key = api_key
session.headers = {'X-Api-Key': api_key}
session.get.return_value = mock
session.get.return_value = mock

self.assertEqual(get_announcements(session), [])

Expand Down Expand Up @@ -511,7 +527,7 @@ def test_get_announcements_wrong_json_response_handling(self, get_used_options):
api_key = "somekey"
session.api_key = api_key
session.headers = {'X-Api-Key': api_key}
session.get.return_value = mock
session.get.return_value = mock

self.assertEqual(get_announcements(session), [])

Expand All @@ -524,7 +540,7 @@ def test_get_announcements_wrong_json_response_handling(self, get_used_options):
api_key = "somekey"
session.api_key = api_key
session.headers = {'X-Api-Key': api_key}
session.get.return_value = mock
session.get.return_value = mock

self.assertEqual(get_announcements(session), [])

Expand Down
Loading