upcdatabase 0.1.1__tar.gz → 0.1.2__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: upcdatabase
3
- Version: 0.1.1
3
+ Version: 0.1.2
4
4
  Summary: Python library for accessing the UPC Database API
5
5
  Author-email: Nick Tak <nickle87@gmail.com>
6
6
  License: MIT
@@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"
4
4
 
5
5
  [project]
6
6
  name = "upcdatabase"
7
- version = "0.1.1"
7
+ version = "0.1.2"
8
8
  description = "Python library for accessing the UPC Database API"
9
9
  readme = "README.md"
10
10
  requires-python = ">=3.7"
@@ -62,13 +62,14 @@ class UPCDatabase:
62
62
  """
63
63
  url = f"{self.BASE_URL}{endpoint}"
64
64
 
65
- # Add API key to params
65
+ # Add bearer token to headers
66
+ headers = {"Authorization": f"Bearer {self.api_key}"}
67
+
66
68
  if params is None:
67
69
  params = {}
68
- params["key"] = self.api_key
69
70
 
70
71
  try:
71
- response = self.session.get(url, params=params, timeout=10)
72
+ response = self.session.get(url, params=params, headers=headers, timeout=10)
72
73
  response.raise_for_status()
73
74
  return response.json()
74
75
  except requests.exceptions.HTTPError as e:
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: upcdatabase
3
- Version: 0.1.1
3
+ Version: 0.1.2
4
4
  Summary: Python library for accessing the UPC Database API
5
5
  Author-email: Nick Tak <nickle87@gmail.com>
6
6
  License: MIT
@@ -11,4 +11,5 @@ src/upcdatabase.egg-info/dependency_links.txt
11
11
  src/upcdatabase.egg-info/requires.txt
12
12
  src/upcdatabase.egg-info/top_level.txt
13
13
  tests/__init__.py
14
- tests/test_client.py
14
+ tests/test_client.py
15
+ tests/test_upc_lookup.py
@@ -75,8 +75,8 @@ class TestUPCDatabase(unittest.TestCase):
75
75
  with UPCDatabase(api_key="test_key") as client:
76
76
  self.assertEqual(client.api_key, "test_key")
77
77
 
78
- def test_api_key_in_params(self):
79
- """Test that API key is added to request params"""
78
+ def test_api_key_in_bearer_token(self):
79
+ """Test that API key is added as bearer token in Authorization header"""
80
80
  with patch("upcdatabase.client.requests.Session.get") as mock_get:
81
81
  mock_response = Mock()
82
82
  mock_response.json.return_value = {}
@@ -84,10 +84,10 @@ class TestUPCDatabase(unittest.TestCase):
84
84
 
85
85
  self.client.lookup("123")
86
86
 
87
- # Check that the get call includes the API key
87
+ # Check that the get call includes the bearer token
88
88
  call_args = mock_get.call_args
89
- self.assertIn("key", call_args[1]["params"])
90
- self.assertEqual(call_args[1]["params"]["key"], self.api_key)
89
+ self.assertIn("headers", call_args[1])
90
+ self.assertEqual(call_args[1]["headers"]["Authorization"], f"Bearer {self.api_key}")
91
91
 
92
92
 
93
93
  class TestUPCDatabaseError(unittest.TestCase):
@@ -0,0 +1,118 @@
1
+ """Test UPC lookup for testing code UPC"""
2
+
3
+ import unittest
4
+ from unittest.mock import Mock, patch
5
+
6
+ from upcdatabase import UPCDatabase
7
+
8
+
9
+ class TestUPCLookup(unittest.TestCase):
10
+ """Test cases for UPC lookup functionality.
11
+
12
+ This test suite validates the UPC lookup endpoint for the specific test UPC code
13
+ 0111222333446, which is a special testing code provided by the UPC Database API.
14
+
15
+ The tests verify:
16
+ - Successful product lookup by UPC code
17
+ - Correct parsing of API response data
18
+ - Bearer token authentication is properly sent
19
+ - API endpoint is called with the correct URL format
20
+ - Response data matches expected structure and values for the testing code
21
+
22
+ Test UPC: 0111222333446
23
+ Product: UPC Database Testing Code (used for testing API integration)
24
+ MSRP: $123.45
25
+ """
26
+
27
+ def setUp(self):
28
+ """Set up test fixtures"""
29
+ self.api_key = "test_api_key"
30
+ self.client = UPCDatabase(api_key=self.api_key)
31
+ self.test_upc = "0111222333446"
32
+ self.expected_response = {
33
+ "added_time": "2011-06-03 19:45:37",
34
+ "modified_time": "2020-03-17 14:59:12",
35
+ "title": "UPC Database Testing Code",
36
+ "alias": "Testing Code",
37
+ "description": "http://upcdatabase.org/code/0111222333446",
38
+ "brand": "",
39
+ "manufacturer": "",
40
+ "msrp": "123.45",
41
+ "ASIN": "",
42
+ "category": "",
43
+ "categories": "",
44
+ "stores": [],
45
+ "barcode": "0111222333446",
46
+ "success": True,
47
+ "timestamp": 1770332489,
48
+ "images": [],
49
+ "metadata": {"msrp": "123.45", "unit": "1 code"},
50
+ "metanutrition": None,
51
+ }
52
+
53
+ @patch("upcdatabase.client.requests.Session.get")
54
+ def test_lookup_testing_upc(self, mock_get):
55
+ """Test lookup of UPC 0111222333446 returns expected data"""
56
+ mock_response = Mock()
57
+ mock_response.json.return_value = self.expected_response
58
+ mock_get.return_value = mock_response
59
+
60
+ result = self.client.lookup(self.test_upc)
61
+
62
+ # Verify the response matches expected
63
+ self.assertEqual(result["barcode"], "0111222333446")
64
+ self.assertEqual(result["title"], "UPC Database Testing Code")
65
+ self.assertEqual(result["alias"], "Testing Code")
66
+ self.assertEqual(result["msrp"], "123.45")
67
+ self.assertTrue(result["success"])
68
+ self.assertEqual(result, self.expected_response)
69
+
70
+ # Verify API was called with correct endpoint
71
+ mock_get.assert_called_once()
72
+ call_args = mock_get.call_args
73
+ self.assertIn("/product/0111222333446", call_args[0][0])
74
+ # Verify bearer token was used
75
+ self.assertIn("Authorization", call_args[1]["headers"])
76
+ self.assertEqual(call_args[1]["headers"]["Authorization"], f"Bearer {self.api_key}")
77
+
78
+ @patch("upcdatabase.client.requests.Session.get")
79
+ def test_bearer_token_in_request(self, mock_get):
80
+ """Test that bearer token is sent in Authorization header.
81
+
82
+ Validates that API requests include the bearer token authentication
83
+ in the Authorization header with the format 'Bearer <api_key>'.
84
+ """
85
+ mock_response = Mock()
86
+ mock_response.json.return_value = {"success": True}
87
+ mock_get.return_value = mock_response
88
+
89
+ self.client.lookup(self.test_upc)
90
+
91
+ # Verify the request was made with Authorization header
92
+ mock_get.assert_called_once()
93
+ call_args = mock_get.call_args
94
+
95
+ # Check that headers were passed
96
+ self.assertIn("headers", call_args[1])
97
+ headers = call_args[1]["headers"]
98
+
99
+ # Check that Authorization header exists
100
+ self.assertIn("Authorization", headers)
101
+
102
+ # Check that bearer token format is correct
103
+ auth_header = headers["Authorization"]
104
+ self.assertTrue(
105
+ auth_header.startswith("Bearer "),
106
+ f"Authorization header should start with 'Bearer ', got: {auth_header}",
107
+ )
108
+
109
+ # Check that API key is included in bearer token
110
+ self.assertEqual(
111
+ auth_header,
112
+ f"Bearer {self.api_key}",
113
+ f"Bearer token should contain API key: {self.api_key}",
114
+ )
115
+
116
+
117
+ if __name__ == "__main__":
118
+ unittest.main()
File without changes
File without changes
File without changes
File without changes
File without changes