11import io
22import pytest
33
4- from unittest .mock import Mock
4+ from unittest .mock import Mock , patch
55from nrfcredstore .credstore import *
66from nrfcredstore .exceptions import ATCommandError
77
@@ -44,10 +44,10 @@ def list_all_resp_blank_lines(self, cred_store):
4444 def ok_resp (self , cred_store ):
4545 cred_store .command_interface .at_command .return_value = True
4646
47- # @pytest.fixture
48- # def csr_resp(self, cred_store):
49- # # KEYGEN value is base64-encoded 'foo' and base64-encoded 'bar' joined by '.'
50- # cred_store.command_interface.at_command .return_value = ['', '%KEYGEN: "Zm9v.YmFy"']
47+ @pytest .fixture
48+ def csr_resp (self , cred_store ):
49+ # KEYGEN value is base64-encoded 'foo' and base64-encoded 'bar' joined by '.'
50+ cred_store .command_interface .get_csr .return_value = "Zm9v.YmFy"
5151
5252 @pytest .fixture
5353 def at_error (self , cred_store ):
@@ -114,8 +114,7 @@ def test_list_fail(self, cred_store, at_error_in_expect_response):
114114 cred_store .list ()
115115
116116 def test_delete_success (self , cred_store , ok_resp ):
117- response = cred_store .delete (567890 , CredType (1 ))
118- assert response == True
117+ cred_store .delete (567890 , CredType (1 ))
119118 self .command_interface .at_command .assert_called_with ('AT%CMNG=3,567890,1' , wait_for_result = True )
120119
121120 def test_delete_fail (self , cred_store , at_error ):
@@ -142,21 +141,24 @@ def test_write_any_type_fail(self, cred_store):
142141 with pytest .raises (ValueError ):
143142 cred_store .write (567890 , CredType .ANY , io .StringIO ())
144143
145- # def test_generate_sends_keygen_cmd(self, cred_store, csr_resp):
146- # fake_binary_file = Mock()
147- # cred_store.keygen(12345678, fake_binary_file)
148- # self.command_interface.at_command .assert_called_with(f'AT%KEYGEN =12345678,2,0', wait_for_result=True )
144+ def test_generate_sends_keygen_cmd (self , cred_store , csr_resp ):
145+ fake_binary_file = Mock ()
146+ cred_store .keygen (12345678 , fake_binary_file )
147+ self .command_interface .get_csr .assert_called_with (sectag = 12345678 , attributes = '' )
149148
150- # def test_generate_with_attributes(self, cred_store, csr_resp):
151- # cred_store.keygen(12345678, Mock(), 'O=Nordic Semiconductor,L=Trondheim,C=no,CN=mydevice')
152- # self.command_interface.at_command.assert_called_with(
153- # f'AT%KEYGEN=12345678,2,0,"O=Nordic Semiconductor,L=Trondheim,C=no,CN=mydevice"')
149+ def test_generate_with_attributes (self , cred_store , csr_resp ):
150+ cred_store .keygen (12345678 , Mock (), 'O=Nordic Semiconductor,L=Trondheim,C=no,CN=mydevice' )
151+ self .command_interface .get_csr .assert_called_with (
152+ sectag = 12345678 ,
153+ attributes = "O=Nordic Semiconductor,L=Trondheim,C=no,CN=mydevice"
154+ )
154155
155- # def test_generate_writes_csr_to_stream(self, cred_store, csr_resp):
156- # fake_binary_file = Mock()
157- # cred_store.keygen(12345678, fake_binary_file)
158- # fake_binary_file.write.assert_called_with(b'foo')
156+ def test_generate_writes_csr_to_stream (self , cred_store , csr_resp ):
157+ fake_binary_file = Mock ()
158+ cred_store .keygen (12345678 , fake_binary_file )
159+ fake_binary_file .write .assert_called_with (b'foo' )
159160
160- def test_generate_fail (self , cred_store , at_error_in_expect_response ):
161- with pytest .raises (RuntimeError ):
162- cred_store .keygen (12345678 , Mock ())
161+ def test_generate_fail (self , cred_store ):
162+ with patch .object (cred_store .command_interface , 'get_csr' , return_value = None ):
163+ with pytest .raises (RuntimeError ):
164+ cred_store .keygen (12345678 , Mock ())
0 commit comments