New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
MYST-224 Refactor identityHandler
, add tests
#89
Changes from all commits
fdf831e
513eb7b
c951db9
82088e6
018975a
30d0397
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
package command_run | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/mysterium/node/identity" | ||
"github.com/mysterium/node/server" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func Test_identityHandler_UseExisting(t *testing.T) { | ||
identityManager := identity.NewIdentityManagerFake() | ||
client := server.NewClientFake() | ||
cache := identity.NewIdentityCacheFake() | ||
|
||
handler := NewNodeIdentityHandler(identityManager, client, cache) | ||
|
||
id, err := handler.UseExisting("address") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. But such value "address" is not mocked in There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This address is ignored in fake identity manager: func (fakeIdm *idmFake) GetIdentity(_ string) (Identity, error) {
return fakeIdm.FakeIdentity1, nil
} There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. But how can we use existing "address" if it dont exist actually. Could could double check, plz There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Double checked - everything seems ok, not sure what's confusing you. |
||
assert.Equal(t, identityManager.FakeIdentity1, id) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could your assert that no identity was registered? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done. |
||
assert.Nil(t, err) | ||
assert.Equal(t, "", client.RegisteredIdentity.Address) | ||
} | ||
|
||
func Test_identityHandler_UseLast(t *testing.T) { | ||
identityManager := identity.NewIdentityManagerFake() | ||
client := server.NewClientFake() | ||
cache := identity.NewIdentityCacheFake() | ||
|
||
fakeIdentity := identity.FromAddress("abc") | ||
cache.StoreIdentity(fakeIdentity) | ||
|
||
handler := NewNodeIdentityHandler(identityManager, client, cache) | ||
|
||
id, err := handler.UseLast() | ||
assert.Equal(t, fakeIdentity, id) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could your assert that no identity was registered? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done. |
||
assert.Nil(t, err) | ||
assert.Equal(t, "", client.RegisteredIdentity.Address) | ||
} | ||
|
||
func Test_identityHandler_UseNew(t *testing.T) { | ||
identityManager := identity.NewIdentityManagerFake() | ||
client := server.NewClientFake() | ||
cache := identity.NewIdentityCacheFake() | ||
|
||
handler := NewNodeIdentityHandler(identityManager, client, cache) | ||
|
||
id, err := handler.UseNew() | ||
assert.Equal(t, identityManager.FakeIdentity2, client.RegisteredIdentity) | ||
assert.Equal(t, identityManager.FakeIdentity2, id) | ||
assert.Nil(t, err) | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,6 +2,7 @@ package identity | |
|
||
import ( | ||
"encoding/json" | ||
"errors" | ||
"io/ioutil" | ||
"os" | ||
"path/filepath" | ||
|
@@ -15,23 +16,24 @@ type IdentityCache struct { | |
File string | ||
} | ||
|
||
func NewIdentityCache(dir string, jsonFile string) *IdentityCache { | ||
func NewIdentityCache(dir string, jsonFile string) IdentityCacheInterface { | ||
return &IdentityCache{ | ||
File: filepath.Join(dir, jsonFile), | ||
} | ||
} | ||
|
||
func (ic *IdentityCache) GetIdentity() (identity Identity, err error) { | ||
if ic.cacheExists() { | ||
cache, err := ic.readCache() | ||
if err != nil { | ||
return identity, err | ||
} | ||
if !ic.cacheExists() { | ||
err = errors.New("cache file does not exist") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's not an error if file is missing. It's same as "identity non found in cache" There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
identity, err = ih.cache.GetIdentity()
if err != nil || !ih.manager.HasIdentity(identity.Address) {
return identity, errors.New("identity not found in cache")
} If cache file does not exist, this results in "identity not found in cache" error either way - with or without this change. |
||
return | ||
} | ||
|
||
return cache.Identity, nil | ||
cache, err := ic.readCache() | ||
if err != nil { | ||
return | ||
} | ||
|
||
return | ||
return cache.Identity, nil | ||
} | ||
|
||
func (ic *IdentityCache) StoreIdentity(identity Identity) error { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package identity | ||
|
||
type identityCacheFake struct { | ||
identity Identity | ||
} | ||
|
||
func NewIdentityCacheFake() IdentityCacheInterface { | ||
return &identityCacheFake{} | ||
} | ||
|
||
func (icf *identityCacheFake) GetIdentity() (identity Identity, err error) { | ||
return icf.identity, nil | ||
} | ||
|
||
func (icf *identityCacheFake) StoreIdentity(identity Identity) error { | ||
icf.identity = identity | ||
return nil | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
package identity | ||
|
||
type IdentityCacheInterface interface { | ||
GetIdentity() (identity Identity, err error) | ||
StoreIdentity(identity Identity) error | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
+100