11"""Test the SSH Keys functionality."""
22
3- from unittest .mock import Mock
3+ from unittest .mock import Mock , patch
44
55import pytest
66
1010 InvalidSSHKeyIDError ,
1111)
1212from pytfe .models .ssh_key import (
13+ SSHKey ,
1314 SSHKeyCreateOptions ,
15+ SSHKeyListOptions ,
1416 SSHKeyUpdateOptions ,
1517)
1618from pytfe .resources .ssh_keys import SSHKeys
@@ -51,7 +53,8 @@ def ssh_keys_service(self):
5153 def test_list_ssh_keys_invalid_org (self , ssh_keys_service ):
5254 """Test listing SSH keys with invalid organization."""
5355 with pytest .raises (InvalidOrgError ):
54- ssh_keys_service .list ("" )
56+ # Need to consume the iterator to trigger the error
57+ list (ssh_keys_service .list ("" ))
5558
5659 def test_create_ssh_key_invalid_org (self , ssh_keys_service ):
5760 """Test creating SSH key with invalid organization."""
@@ -74,3 +77,61 @@ def test_delete_ssh_key_invalid_id(self, ssh_keys_service):
7477 """Test deleting SSH key with invalid ID."""
7578 with pytest .raises (InvalidSSHKeyIDError ):
7679 ssh_keys_service .delete ("" )
80+
81+ def test_list_ssh_keys_success (self , ssh_keys_service ):
82+ """Test successful list operation with iterator."""
83+ mock_list_data = [
84+ {
85+ "id" : "sshkey-123" ,
86+ "attributes" : {
87+ "name" : "Test SSH Key 1" ,
88+ },
89+ },
90+ {
91+ "id" : "sshkey-456" ,
92+ "attributes" : {
93+ "name" : "Test SSH Key 2" ,
94+ },
95+ },
96+ ]
97+
98+ with patch .object (ssh_keys_service , "_list" ) as mock_list :
99+ mock_list .return_value = mock_list_data
100+
101+ # Test with options
102+ options = SSHKeyListOptions (page_number = 1 , page_size = 5 )
103+ result = list (ssh_keys_service .list ("test-org" , options ))
104+
105+ # Verify _list was called with correct path
106+ assert mock_list .call_count == 1
107+ call_args = mock_list .call_args
108+ assert call_args [0 ][0 ] == "/api/v2/organizations/test-org/ssh-keys"
109+
110+ # Verify params structure includes pagination and options
111+ params = call_args [1 ]["params" ]
112+ assert "page[number]" in params
113+ assert "page[size]" in params
114+
115+ # Verify result structure - iterator yields SSHKey objects
116+ assert len (result ) == 2
117+
118+ # Verify SSH key objects were created correctly from response data
119+ key1 = result [0 ]
120+ assert isinstance (key1 , SSHKey )
121+ assert key1 .id == "sshkey-123"
122+ assert key1 .name == "Test SSH Key 1"
123+
124+ key2 = result [1 ]
125+ assert isinstance (key2 , SSHKey )
126+ assert key2 .id == "sshkey-456"
127+ assert key2 .name == "Test SSH Key 2"
128+
129+ def test_list_ssh_keys_empty (self , ssh_keys_service ):
130+ """Test list operation returning empty results."""
131+ with patch .object (ssh_keys_service , "_list" ) as mock_list :
132+ mock_list .return_value = []
133+
134+ result = list (ssh_keys_service .list ("test-org" ))
135+
136+ assert len (result ) == 0
137+ mock_list .assert_called_once ()
0 commit comments