1+ import pytest
2+ from unittest .mock import patch , MagicMock
3+ import digitalocean
4+ from cloudproxy .providers .digitalocean .functions import (
5+ delete_proxy ,
6+ create_firewall ,
7+ DOFirewallExistsException
8+ )
9+
10+
11+ class TestDeleteProxyErrorHandling :
12+ """Tests for error handling in delete_proxy function."""
13+
14+ @patch ('cloudproxy.providers.digitalocean.functions.get_manager' )
15+ def test_delete_proxy_with_droplet_object (self , mock_get_manager ):
16+ """Test delete_proxy when called with a droplet object instead of just ID."""
17+ # Create a mock droplet object
18+ mock_droplet = MagicMock ()
19+ mock_droplet .id = 12345
20+
21+ # Mock the manager and its methods
22+ mock_manager = MagicMock ()
23+ mock_manager .get_droplet .return_value = mock_droplet
24+ mock_get_manager .return_value = mock_manager
25+
26+ # Mock the destroy method
27+ mock_droplet .destroy .return_value = True
28+
29+ # Call the function with the droplet object
30+ result = delete_proxy (mock_droplet )
31+
32+ # Verify the right methods were called
33+ mock_get_manager .assert_called_once ()
34+ mock_manager .get_droplet .assert_called_once_with (12345 )
35+ assert result == True
36+
37+ @patch ('cloudproxy.providers.digitalocean.functions.get_manager' )
38+ def test_delete_proxy_droplet_not_found (self , mock_get_manager ):
39+ """Test delete_proxy when the droplet is not found."""
40+ # Mock the manager
41+ mock_manager = MagicMock ()
42+ # Make get_droplet raise an exception with "not found" in the message
43+ mock_manager .get_droplet .side_effect = Exception ("Droplet not found" )
44+ mock_get_manager .return_value = mock_manager
45+
46+ # Call the function
47+ result = delete_proxy (12345 )
48+
49+ # Verify it considers a missing droplet as successfully deleted
50+ mock_manager .get_droplet .assert_called_once_with (12345 )
51+ assert result == True
52+
53+ @patch ('cloudproxy.providers.digitalocean.functions.get_manager' )
54+ def test_delete_proxy_with_droplet_object_not_found (self , mock_get_manager ):
55+ """Test delete_proxy with a droplet object when the droplet is not found."""
56+ # Create a mock droplet object
57+ mock_droplet = MagicMock ()
58+ mock_droplet .id = 12345
59+
60+ # Mock the manager
61+ mock_manager = MagicMock ()
62+ # Make get_droplet raise an exception with "not found" in the message
63+ mock_manager .get_droplet .side_effect = Exception ("Droplet not found" )
64+ mock_get_manager .return_value = mock_manager
65+
66+ # Call the function with the droplet object
67+ result = delete_proxy (mock_droplet )
68+
69+ # Verify it considers a missing droplet as successfully deleted
70+ mock_manager .get_droplet .assert_called_once_with (12345 )
71+ assert result == True
72+
73+ @patch ('cloudproxy.providers.digitalocean.functions.get_manager' )
74+ def test_delete_proxy_with_error_in_destroy (self , mock_get_manager ):
75+ """Test delete_proxy when the destroy method raises an exception."""
76+ # Create mock droplet and manager
77+ mock_droplet = MagicMock ()
78+ mock_manager = MagicMock ()
79+ mock_manager .get_droplet .return_value = mock_droplet
80+ mock_get_manager .return_value = mock_manager
81+
82+ # Make destroy raise a non-404 exception
83+ mock_droplet .destroy .side_effect = Exception ("Some other error" )
84+
85+ # Call the function and expect the exception to be raised
86+ with pytest .raises (Exception , match = "Some other error" ):
87+ delete_proxy (12345 )
88+
89+ # Verify the right methods were called
90+ mock_manager .get_droplet .assert_called_once ()
91+ mock_droplet .destroy .assert_called_once ()
92+
93+ @patch ('cloudproxy.providers.digitalocean.functions.get_manager' )
94+ def test_delete_proxy_with_404_in_destroy (self , mock_get_manager ):
95+ """Test delete_proxy when the destroy method raises a 404 exception."""
96+ # Create mock droplet and manager
97+ mock_droplet = MagicMock ()
98+ mock_manager = MagicMock ()
99+ mock_manager .get_droplet .return_value = mock_droplet
100+ mock_get_manager .return_value = mock_manager
101+
102+ # Make destroy raise a 404 exception
103+ mock_droplet .destroy .side_effect = Exception ("404 Not Found" )
104+
105+ # Call the function
106+ result = delete_proxy (12345 )
107+
108+ # Verify it treats 404 as success
109+ mock_manager .get_droplet .assert_called_once ()
110+ mock_droplet .destroy .assert_called_once ()
111+ assert result == True
0 commit comments