Coverage for tests/endpoints/tests_snaps.py: 100%
59 statements
« prev ^ index » next coverage.py v7.10.2, created at 2025-08-05 22:06 +0000
« prev ^ index » next coverage.py v7.10.2, created at 2025-08-05 22:06 +0000
1from unittest.mock import patch
2from tests.endpoints.endpoint_testing import TestEndpoints
5class TestGetStoreSnaps(TestEndpoints):
6 @patch("canonicalwebteam.store_api.dashboard.Dashboard.get_store_snaps")
7 @patch("canonicalwebteam.store_api.dashboard.Dashboard.get_store")
8 def test_get_store_snaps_without_whitelist(
9 self, mock_get_store, mock_get_store_snaps
10 ):
11 """Test getting store snaps when store has no whitelist"""
12 mock_store = {"id": "test-store-id", "name": "Test Store"}
13 mock_get_store.return_value = mock_store
15 mock_snaps = [
16 {"snap-id": "snap1", "name": "test-snap-1"},
17 {"snap-id": "snap2", "name": "test-snap-2"},
18 ]
19 mock_get_store_snaps.return_value = mock_snaps
21 response = self.client.get("/api/store/test-store-id/snaps")
22 data = response.get_json()
24 self.assertEqual(response.status_code, 200)
25 self.assertEqual(data, mock_snaps)
27 @patch("canonicalwebteam.store_api.dashboard.Dashboard.get_store_snaps")
28 @patch("canonicalwebteam.store_api.dashboard.Dashboard.get_store")
29 def test_get_store_snaps_with_whitelist(
30 self, mock_get_store, mock_get_store_snaps
31 ):
32 """Test getting store snaps when store has whitelist with accessible
33 stores"""
34 mock_store = {
35 "id": "test-store-id",
36 "name": "Test Store",
37 "store-whitelist": ["included-store-1", "included-store-2"],
38 }
39 mock_get_store.side_effect = [
40 mock_store,
41 {"id": "included-store-1", "name": "Included Store 1"},
42 {"id": "included-store-2", "name": "Included Store 2"},
43 ]
45 mock_snaps = [{"snap-id": "snap1", "name": "test-snap-1"}]
46 mock_get_store_snaps.return_value = mock_snaps
48 response = self.client.get("/api/store/test-store-id/snaps")
49 data = response.get_json()
51 self.assertEqual(response.status_code, 200)
52 self.assertEqual(len(data), 2)
53 self.assertEqual(data[0], mock_snaps[0])
55 included_stores_data = data[1]
56 self.assertIn("included-stores", included_stores_data)
57 included_stores = included_stores_data["included-stores"]
58 self.assertEqual(len(included_stores), 2)
60 self.assertEqual(included_stores[0]["id"], "included-store-1")
61 self.assertEqual(included_stores[0]["name"], "Included Store 1")
62 self.assertTrue(included_stores[0]["userHasAccess"])
64 self.assertEqual(included_stores[1]["id"], "included-store-2")
65 self.assertEqual(included_stores[1]["name"], "Included Store 2")
66 self.assertTrue(included_stores[1]["userHasAccess"])
69class TestPostManageStoreSnaps(TestEndpoints):
70 @patch("canonicalwebteam.store_api.dashboard.Dashboard.update_store_snaps")
71 def test_post_manage_store_snaps_success(self, mock_update_store_snaps):
72 """Test successfully updating store snaps"""
73 mock_update_store_snaps.return_value = None
75 test_snaps = [
76 {"snap-id": "snap1", "name": "test-snap-1"},
77 {"snap-id": "snap2", "name": "test-snap-2"},
78 ]
80 response = self.client.post(
81 "/api/store/test-store-id/snaps",
82 data={"snaps": str(test_snaps).replace("'", '"')},
83 )
84 data = response.get_json()
86 self.assertEqual(response.status_code, 200)
87 self.assertTrue(data["success"])
88 mock_update_store_snaps.assert_called_once()
90 @patch("canonicalwebteam.store_api.dashboard.Dashboard.update_store_snaps")
91 def test_post_manage_store_snaps_with_json_string(
92 self, mock_update_store_snaps
93 ):
94 """Test updating store snaps with proper JSON string"""
95 mock_update_store_snaps.return_value = None
97 import json
99 test_snaps = [
100 {"snap-id": "snap1", "name": "test-snap-1", "enabled": True},
101 {"snap-id": "snap2", "name": "test-snap-2", "enabled": False},
102 ]
104 response = self.client.post(
105 "/api/store/test-store-id/snaps",
106 data={"snaps": json.dumps(test_snaps)},
107 )
108 data = response.get_json()
110 self.assertEqual(response.status_code, 200)
111 self.assertTrue(data["success"])
113 # Verify the dashboard method was called with the correct arguments
114 mock_update_store_snaps.assert_called_once()
115 call_args = mock_update_store_snaps.call_args
116 self.assertEqual(call_args[0][1], "test-store-id") # store_id
117 self.assertEqual(call_args[0][2], test_snaps) # snaps data