File size: 1,119 Bytes
7500cab
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
import sys
from pathlib import Path

sys.path.append(str(Path(__file__).resolve().parents[1]))

import unittest
import json
from src.inference.app import app

class APITestCase(unittest.TestCase):
    """
    Unit test for the phishing URL prediction API.
    """

    def setUp(self):
        """Creating a Flask test client before each test."""
        self.client = app.test_client()

    def test_predict(self):
        """Ensuring prediction endpoint returns a phishing score""" 
        response = self.client.post(
            "/predict",
            json = {"url" : "www.dghjdgf.com/paypal.co.uk/cycgi-bin/webscrcmd=_home-customer&nav=1/loading.php"}
        )       

        self.assertEqual(response.status_code, 200)
        data = response.get_json()
        self.assertIn("phishing", data)
        self.assertIsInstance(data["phishing"], float)

    def test_missing_url(self):
        """Ensuring API returns an error when 'url' is missing"""
        response = self.client.post("/predict", json = {})
        self.assertNotEqual(response.status_code, 200)

if __name__ == "__main__":
    unittest.main()