File tree Expand file tree Collapse file tree 2 files changed +8
-5
lines changed Expand file tree Collapse file tree 2 files changed +8
-5
lines changed Original file line number Diff line number Diff line change @@ -73,7 +73,7 @@ def user_exists(app, mail):
73
73
:raises: GpgErrorException
74
74
75
75
"""
76
- cmd = gpg_cmd + ["--list-secret-keys " , "--with-colons " , "<{0}>" .format (mail )]
76
+ cmd = gpg_cmd + ["--armor" , "--batch " , "--export " , "<{0}>" .format (mail )]
77
77
78
78
try :
79
79
handle = Popen (cmd , stdout = PIPE , stderr = PIPE )
@@ -83,12 +83,12 @@ def user_exists(app, mail):
83
83
raise GpgErrorException (msg = "unhandled exception during gpg call" ,
84
84
cmd = " " .join (cmd ), err = e )
85
85
86
- if handle . returncode == 0 :
86
+ if "BEGIN PGP PUBLIC KEY BLOCK" in stdout . decode ( "utf-8" ) :
87
87
# TODO: validate that the key is ultimately trusted
88
88
log .debug ("user {} has keys in keyring" .format (mail ))
89
89
ensure_passphrase_exist (app , mail )
90
90
return True
91
- elif "error reading key " in stderr .decode ():
91
+ elif "nothing exported " in stderr .decode ("utf-8" ):
92
92
log .debug ("user {} not found in keyring" .format (mail ))
93
93
return False
94
94
else :
Original file line number Diff line number Diff line change @@ -103,12 +103,15 @@ def communicate(self):
103
103
@mock .patch ("copr_keygen.logic.Popen" )
104
104
class TestUserExists (TestCase ):
105
105
def test_exists (self , popen , ensure_passphrase ):
106
- popen .return_value = MockPopenHandle (0 )
106
+ stdout = "-----BEGIN PGP PUBLIC KEY BLOCK-----\n \n mQENB..."
107
+ popen .return_value = MockPopenHandle (stdout = stdout )
107
108
ensure_passphrase .return_value = True
108
109
assert logic .user_exists (app , TEST_EMAIL )
109
110
110
111
def test_not_exists (self , popen , ensure_passphrase ):
111
- popen .return_value = MockPopenHandle (1 , stderr = "error reading key" )
112
+ # The exit code for the GPG command is zero even on failure
113
+ stderr = "gpg: WARNING: nothing exported"
114
+ popen .return_value = MockPopenHandle (0 , stderr = stderr )
112
115
ensure_passphrase .return_value = True
113
116
assert not logic .user_exists (app , TEST_EMAIL )
114
117
You can’t perform that action at this time.
0 commit comments