Skip to content

Commit

Permalink
#43 #44 - Adding functionality to show a notification saying that is …
Browse files Browse the repository at this point in the history
…not available a private o public key when only a public or private key is available
  • Loading branch information
piratax007 committed Aug 6, 2022
1 parent 5960fe6 commit 716b1b7
Show file tree
Hide file tree
Showing 5 changed files with 91 additions and 26 deletions.
47 changes: 33 additions & 14 deletions gui/definitions/interface/KeyDetails.xml
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,29 @@
</packing>
</child>
<child>
<!-- n-columns=2 n-rows=7 -->
<!-- n-columns=2 n-rows=8 -->
<object class="GtkGrid" id="keyDetailsGrid">
<property name="visible">True</property>
<property name="can-focus">False</property>
<child>
<object class="GtkLabel" id="notification">
<property name="visible">True</property>
<property name="can-focus">False</property>
<property name="halign">start</property>
<property name="width-chars">20</property>
<property name="selectable">True</property>
<style>
<class name="notification"/>
</style>
</object>
<packing>
<property name="left-attach">1</property>
<property name="top-attach">0</property>
</packing>
</child>
<style>
<class name="publicKey"/>
</style>
<child>
<object class="GtkLabel" id="publicKeyPathLabel">
<property name="visible">True</property>
Expand All @@ -38,7 +57,7 @@
</object>
<packing>
<property name="left-attach">0</property>
<property name="top-attach">0</property>
<property name="top-attach">1</property>
</packing>
</child>
<child>
Expand All @@ -55,7 +74,7 @@
</object>
<packing>
<property name="left-attach">1</property>
<property name="top-attach">0</property>
<property name="top-attach">1</property>
</packing>
</child>
<style>
Expand All @@ -73,7 +92,7 @@
</object>
<packing>
<property name="left-attach">0</property>
<property name="top-attach">1</property>
<property name="top-attach">2</property>
</packing>
</child>
<child>
Expand All @@ -90,7 +109,7 @@
</object>
<packing>
<property name="left-attach">1</property>
<property name="top-attach">1</property>
<property name="top-attach">2</property>
</packing>
</child>
<style>
Expand All @@ -110,7 +129,7 @@
</object>
<packing>
<property name="left-attach">1</property>
<property name="top-attach">2</property>
<property name="top-attach">3</property>
</packing>
</child>
<style>
Expand All @@ -128,7 +147,7 @@
</object>
<packing>
<property name="left-attach">0</property>
<property name="top-attach">3</property>
<property name="top-attach">4</property>
</packing>
</child>
<child>
Expand All @@ -143,7 +162,7 @@
</object>
<packing>
<property name="left-attach">1</property>
<property name="top-attach">3</property>
<property name="top-attach">4</property>
</packing>
</child>
<style>
Expand All @@ -161,7 +180,7 @@
</object>
<packing>
<property name="left-attach">0</property>
<property name="top-attach">4</property>
<property name="top-attach">5</property>
</packing>
</child>
<child>
Expand All @@ -176,7 +195,7 @@
</object>
<packing>
<property name="left-attach">1</property>
<property name="top-attach">4</property>
<property name="top-attach">5</property>
</packing>
</child>
<style>
Expand All @@ -194,7 +213,7 @@
</object>
<packing>
<property name="left-attach">0</property>
<property name="top-attach">5</property>
<property name="top-attach">6</property>
</packing>
</child>
<child>
Expand All @@ -211,7 +230,7 @@
</object>
<packing>
<property name="left-attach">1</property>
<property name="top-attach">5</property>
<property name="top-attach">6</property>
</packing>
</child>
<style>
Expand All @@ -229,7 +248,7 @@
</object>
<packing>
<property name="left-attach">0</property>
<property name="top-attach">6</property>
<property name="top-attach">7</property>
</packing>
</child>
<child>
Expand All @@ -246,7 +265,7 @@
</object>
<packing>
<property name="left-attach">1</property>
<property name="top-attach">6</property>
<property name="top-attach">7</property>
</packing>
</child>
<style>
Expand Down
2 changes: 1 addition & 1 deletion gui/definitions/styles/global.css
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ window {
padding-right: 10px;
}

.passwordProtected {
.keyDetail .passwordProtected, .keyDetail .notification {
font-style: italic;
color: @theme_unfocused_fg_color;
}
Expand Down
26 changes: 26 additions & 0 deletions gui/key_details.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"fmt"
"github.com/coyim/gotk3adapter/gtki"
"github.com/digitalautonomy/keymirror/api"
"github.com/digitalautonomy/keymirror/i18n"
"strings"
)

Expand Down Expand Up @@ -171,7 +172,32 @@ func (kd *keyDetails) displayUserID() {
}
}

const notificationIdentifier = "notification"

func (kd *keyDetails) displayNotification(message string) {
label := kd.builder.get(notificationIdentifier).(gtki.Label)
label.SetLabel(message)
}

func (kd *keyDetails) hideNotificationRow() {
kd.hide(notificationIdentifier)
}

func (kd *keyDetails) displayPotentialNotification() {
switch kd.key.KeyType() {
case api.PublicKeyType:
kd.displayNotification(i18n.Local("(no private key available)"))
case api.PrivateKeyType:
kd.displayNotification(i18n.Local("(no public key available)"))
case api.PairKeyType:
fallthrough
default:
kd.hideNotificationRow()
}
}

func (kd *keyDetails) display() {
kd.displayPotentialNotification()
kd.displayLocations(kd.key.PublicKeyLocations(), publicKeyPath, publicKeyPathLabel)
kd.displayLocations(kd.key.PrivateKeyLocations(), privateKeyPath, privateKeyPathLabel)
kd.displayIsPasswordProtected()
Expand Down
29 changes: 22 additions & 7 deletions gui/key_details_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,15 @@ func (s *guiSuite) Test_populateKeyDetails_createsTheKeyDetailsBoxAndDisplaysThe
pathPublicKeyPath := &gtk.MockLabel{}
builderKeyDetailsBoxMock.On("GetObject", "publicKeyPath").Return(pathPublicKeyPath, nil).Once()

s.addLabelsThatShouldHide(builderKeyDetailsBoxMock, "privateKeyPathLabel", "privateKeyPath", "passwordProtectedLabel")
s.addLabelsThatShouldHide(builderKeyDetailsBoxMock,
"privateKeyPathLabel",
"privateKeyPath",
"passwordProtectedLabel",
)

notificationMessage := &gtk.MockLabel{}
builderKeyDetailsBoxMock.On("GetObject", "notification").Return(notificationMessage, nil).Once()
notificationMessage.On("SetLabel", "(no private key available)").Return().Once()

textProperties := &gtk.MockLabel{}
builderKeyDetailsBoxMock.On("GetObject", "securityProperties").Return(textProperties, nil).Once()
Expand All @@ -42,15 +50,15 @@ func (s *guiSuite) Test_populateKeyDetails_createsTheKeyDetailsBoxAndDisplaysThe
builderKeyDetailsBoxMock.On("GetObject", "sha256Fingerprint").Return(fingerprintSha256, nil).Once()

UserIDValue := &gtk.MockLabel{}
builderKeyDetailsBoxMock.On("GetObject", "userIDIdentifier").Return(UserIDValue, nil).Once()
builderKeyDetailsBoxMock.On("GetObject", "userID").Return(UserIDValue, nil).Once()
UserIDValue.On("SetLabel", "").Return().Once()

keMock := &publicKeyEntryMock{}
keMock.On("WithDigestContent", mock.Anything).Return([]byte{0xAB, 0xCD, 0x10}).Once()
keMock.On("WithDigestContent", mock.Anything).Return([]byte{0xCC, 0x07, 0x00, 0xFF}).Once()
keMock.On("PublicKeyLocations").Return([]string{"/a/path/to/a/public/key"}).Once()
keMock.On("PrivateKeyLocations").Return(nil).Once()
keMock.On("KeyType").Return(api.PublicKeyType).Once()
keMock.On("KeyType").Return(api.PublicKeyType).Maybe()
keMock.On("Algorithm").Return(api.Ed25519).Once()
keMock.On("UserID").Return("").Once()
pathPublicKeyPath.On("SetLabel", "/a/path/to/a/public/key").Return().Once()
Expand All @@ -69,6 +77,7 @@ func (s *guiSuite) Test_populateKeyDetails_createsTheKeyDetailsBoxAndDisplaysThe

keyDetailsHolder.AssertExpectations(s.T())
keMock.AssertExpectations(s.T())
notificationMessage.AssertExpectations(s.T())
fingerprintSha1.AssertExpectations(s.T())
fingerprintSha256.AssertExpectations(s.T())
UserIDValue.AssertExpectations(s.T())
Expand All @@ -91,21 +100,25 @@ func (s *guiSuite) Test_populateKeyDetails_createsTheKeyDetailsBoxAndDisplaysThe
"publicKeyPathLabel",
"publicKeyPath",
"userIDLabel",
"userIDIdentifier",
"userID",
"sha1FingerprintLabel",
"sha1Fingerprint",
"sha256FingerprintLabel",
"sha256Fingerprint",
)

notificationMessage := &gtk.MockLabel{}
builderKeyDetailsBoxMock.On("GetObject", "notification").Return(notificationMessage, nil).Once()
notificationMessage.On("SetLabel", "(no public key available)").Return().Once()

textProperties := &gtk.MockLabel{}
builderKeyDetailsBoxMock.On("GetObject", "securityProperties").Return(textProperties, nil).Once()
textProperties.On("SetLabel", "Ed25519").Return().Once()

keMock := &keyEntryMock{}
keMock.On("PublicKeyLocations").Return(nil).Once()
keMock.On("PrivateKeyLocations").Return([]string{"/a/path/to/a/private/key"}).Once()
keMock.On("KeyType").Return(api.PrivateKeyType).Once()
keMock.On("KeyType").Return(api.PrivateKeyType).Maybe()
keMock.On("Algorithm").Return(api.Ed25519).Once()
pathPrivateKey.On("SetLabel", "/a/path/to/a/private/key").Return().Once()
pathPrivateKey.On("SetTooltipText", "/a/path/to/a/private/key").Return().Once()
Expand All @@ -116,6 +129,7 @@ func (s *guiSuite) Test_populateKeyDetails_createsTheKeyDetailsBoxAndDisplaysThe
u.populateKeyDetails(keMock, keyDetailsHolder)

keyDetailsHolder.AssertExpectations(s.T())
notificationMessage.AssertExpectations(s.T())
keMock.AssertExpectations(s.T())
pathPrivateKey.AssertExpectations(s.T())
textProperties.AssertExpectations(s.T())
Expand All @@ -139,11 +153,12 @@ func (s *guiSuite) Test_populateKeyDetails_createsTheKeyDetailsBoxAndDisplaysBot
s.addLabelsThatShouldHide(builderKeyDetailsBoxMock,
"passwordProtectedLabel",
"userIDLabel",
"userIDIdentifier",
"userID",
"sha1FingerprintLabel",
"sha1Fingerprint",
"sha256FingerprintLabel",
"sha256Fingerprint",
"notification",
)

properties := &gtk.MockLabel{}
Expand All @@ -153,7 +168,7 @@ func (s *guiSuite) Test_populateKeyDetails_createsTheKeyDetailsBoxAndDisplaysBot
keMock := &keyEntryMock{}
keMock.On("PublicKeyLocations").Return([]string{"/a/path/to/a/public/key"}).Once()
keMock.On("PrivateKeyLocations").Return([]string{"/a/path/to/a/private/key"}).Once()
keMock.On("KeyType").Return(api.PairKeyType).Once()
keMock.On("KeyType").Return(api.PairKeyType).Maybe()
keMock.On("Algorithm").Return(api.Ed25519).Once()
pathPublicKey.On("SetLabel", "/a/path/to/a/public/key").Return().Once()
pathPublicKey.On("SetTooltipText", "/a/path/to/a/public/key").Return().Once()
Expand Down
13 changes: 9 additions & 4 deletions gui/key_list_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ func (pk *publicKeyEntryMock) WithDigestContent(f func([]byte) []byte) []byte {

func (pk *publicKeyEntryMock) UserID() string {
returns := pk.Called()
return ret[string](returns, 0)
return returns.String(0)
}

func (s *guiSuite) Test_createKeyEntryBoxFrom_CreatesAGTKIBoxWithTheGivenASSHKeyEntry() {
Expand Down Expand Up @@ -108,20 +108,24 @@ func (s *guiSuite) Test_createKeyEntryBoxFrom_CreatesAGTKIBoxWithTheGivenASSHKey
"sha1Fingerprint",
"sha256FingerprintLabel",
"sha256Fingerprint",
"userIDLabelIdentifier",
"userIDIdentifier",
"userIDLabel",
"userID",
)

keyEntry.On("PublicKeyLocations").Return([]string{"/a/path/to/a/public/key"}).Once()
keyEntry.On("PrivateKeyLocations").Return(nil).Once()
keyEntry.On("KeyType").Return(api.PublicKeyType).Once()
keyEntry.On("KeyType").Return(api.PublicKeyType).Maybe()

pathPublicKey.On("SetLabel", "/a/path/to/a/public/key").Return().Once()
pathPublicKey.On("SetTooltipText", "/a/path/to/a/public/key").Return().Once()

detailsRevMock.On("Show").Return().Once()
detailsRevMock.On("SetRevealChild", true).Return().Once()

notificationMessage := &gtk.MockLabel{}
builderKeyDetailsBoxMock.On("GetObject", "notification").Return(notificationMessage, nil).Once()
notificationMessage.On("SetLabel", "(no private key available)").Return().Once()

properties := &gtk.MockLabel{}
builderKeyDetailsBoxMock.On("GetObject", "securityProperties").Return(properties, nil).Once()
properties.On("SetLabel", "Ed25519").Return().Once()
Expand All @@ -134,6 +138,7 @@ func (s *guiSuite) Test_createKeyEntryBoxFrom_CreatesAGTKIBoxWithTheGivenASSHKey
s.Equal(1, onWindowChangeCalled)
s.Equal(box, actualGtkBox)
keyEntry.AssertExpectations(s.T())
notificationMessage.AssertExpectations(s.T())
detailsBoxMock.AssertExpectations(s.T())
detailsRevMock.AssertExpectations(s.T())
scMock1.AssertExpectations(s.T())
Expand Down

0 comments on commit 716b1b7

Please sign in to comment.