Skip to content

Commit

Permalink
remove mock provider changes
Browse files Browse the repository at this point in the history
Signed-off-by: AlanD20 <aland20@pm.me>
  • Loading branch information
AlanD20 committed May 31, 2024
1 parent 14b7cd2 commit 69620c4
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 69 deletions.
58 changes: 7 additions & 51 deletions keyring_mock.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,7 @@
package keyring

type mockProviderItem struct {
Value string
Locked bool
}

type mockProvider struct {
mockStore map[string]map[string]*mockProviderItem
mockStore map[string]map[string]string
mockError error
}

Expand All @@ -17,12 +12,12 @@ func (m *mockProvider) Set(service, user, pass string) error {
return m.mockError
}
if m.mockStore == nil {
m.mockStore = make(map[string]map[string]*mockProviderItem)
m.mockStore = make(map[string]map[string]string)
}
if m.mockStore[service] == nil {
m.mockStore[service] = make(map[string]*mockProviderItem)
m.mockStore[service] = make(map[string]string)
}
m.mockStore[service][user] = &mockProviderItem{Value: pass, Locked: true}
m.mockStore[service][user] = pass
return nil
}

Expand All @@ -32,12 +27,8 @@ func (m *mockProvider) Get(service, user string) (string, error) {
return "", m.mockError
}
if b, ok := m.mockStore[service]; ok {
if item, ok := b[user]; ok {
if item.Locked {
return "", ErrNotFound
}
_ = m.Lock(service, user)
return item.Value, nil
if v, ok := b[user]; ok {
return v, nil
}
}
return "", ErrNotFound
Expand All @@ -50,10 +41,7 @@ func (m *mockProvider) Delete(service, user string) error {
}
if m.mockStore != nil {
if _, ok := m.mockStore[service]; ok {
if item, ok := m.mockStore[service][user]; ok {
if item.Locked {
return ErrNotFound
}
if _, ok := m.mockStore[service][user]; ok {
delete(m.mockStore[service], user)
return nil
}
Expand All @@ -62,38 +50,6 @@ func (m *mockProvider) Delete(service, user string) error {
return ErrNotFound
}

// Unlock unlocks item from the keyring given a service name and a user
func (m *mockProvider) Unlock(service, user string) error {
if m.mockError != nil {
return m.mockError
}
if m.mockStore != nil {
if _, ok := m.mockStore[service]; ok {
if item, ok := m.mockStore[service][user]; ok {
item.Locked = false
return nil
}
}
}
return ErrNotFound
}

// Lock locks item from the keyring given a service name and a user
func (m *mockProvider) Lock(service, user string) error {
if m.mockError != nil {
return m.mockError
}
if m.mockStore != nil {
if _, ok := m.mockStore[service]; ok {
if item, ok := m.mockStore[service][user]; ok {
item.Locked = true
return nil
}
}
}
return ErrNotFound
}

// MockInit sets the provider to a mocked memory store
func MockInit() {
provider = &mockProvider{}
Expand Down
18 changes: 0 additions & 18 deletions keyring_mock_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ func TestMockGet(t *testing.T) {
t.Errorf("Should not fail, got: %s", err)
}

_ = mp.Unlock(service, user)
pw, err := mp.Get(service, user)
if err != nil {
t.Errorf("Should not fail, got: %s", err)
Expand All @@ -33,22 +32,6 @@ func TestMockGet(t *testing.T) {
}
}

// TestGetLocked tests getting a locked password from the keyring.
func TestMockGetLocked(t *testing.T) {
mp := mockProvider{}
err := mp.Set(service, user, password)
if err != nil {
t.Errorf("Should not fail, got: %s", err)
}

pwd, err := mp.Get(service, user)
assertError(t, err, ErrNotFound)

if pwd != "" {
t.Errorf("Should not return item value, got: %s", pwd)
}
}

// TestGetNonExisting tests getting a secret not in the keyring.
func TestMockGetNonExisting(t *testing.T) {
mp := mockProvider{}
Expand All @@ -66,7 +49,6 @@ func TestMockDelete(t *testing.T) {
t.Errorf("Should not fail, got: %s", err)
}

_ = mp.Unlock(service, user)
err = mp.Delete(service, user)
if err != nil {
t.Errorf("Should not fail, got: %s", err)
Expand Down

0 comments on commit 69620c4

Please sign in to comment.