2018-11-21 20:56:12 +03:00
|
|
|
package identity
|
|
|
|
|
|
|
|
import (
|
2019-02-01 14:22:00 +03:00
|
|
|
"encoding/json"
|
2018-11-21 20:56:12 +03:00
|
|
|
"testing"
|
|
|
|
|
2020-11-19 15:57:57 +03:00
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
|
2019-08-11 15:08:03 +03:00
|
|
|
"github.com/MichaelMure/git-bug/entity"
|
2018-11-21 20:56:12 +03:00
|
|
|
"github.com/MichaelMure/git-bug/repository"
|
|
|
|
)
|
|
|
|
|
2019-01-20 17:41:27 +03:00
|
|
|
// Test the commit and load of an Identity with multiple versions
|
2019-01-17 04:05:50 +03:00
|
|
|
func TestIdentityCommitLoad(t *testing.T) {
|
2020-11-08 19:54:28 +03:00
|
|
|
mockRepo := repository.NewMockRepo()
|
2018-11-21 20:56:12 +03:00
|
|
|
|
|
|
|
// single version
|
|
|
|
|
2019-01-20 17:41:27 +03:00
|
|
|
identity := &Identity{
|
2019-08-11 15:08:03 +03:00
|
|
|
id: entity.UnsetId,
|
2019-02-07 00:06:42 +03:00
|
|
|
versions: []*Version{
|
2018-11-21 20:56:12 +03:00
|
|
|
{
|
2019-02-07 00:06:42 +03:00
|
|
|
name: "René Descartes",
|
|
|
|
email: "rene.descartes@example.com",
|
2018-11-21 20:56:12 +03:00
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
err := identity.Commit(mockRepo)
|
|
|
|
|
2020-11-19 15:57:57 +03:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.NotEmpty(t, identity.id)
|
2018-11-21 20:56:12 +03:00
|
|
|
|
2019-02-03 21:55:35 +03:00
|
|
|
loaded, err := ReadLocal(mockRepo, identity.id)
|
2020-11-19 15:57:57 +03:00
|
|
|
require.NoError(t, err)
|
2019-01-17 04:05:50 +03:00
|
|
|
commitsAreSet(t, loaded)
|
2020-11-19 15:57:57 +03:00
|
|
|
require.Equal(t, identity, loaded)
|
2019-01-17 04:05:50 +03:00
|
|
|
|
2018-11-21 20:56:12 +03:00
|
|
|
// multiple version
|
|
|
|
|
2019-01-20 17:41:27 +03:00
|
|
|
identity = &Identity{
|
2019-08-11 15:08:03 +03:00
|
|
|
id: entity.UnsetId,
|
2019-02-07 00:06:42 +03:00
|
|
|
versions: []*Version{
|
2018-11-21 20:56:12 +03:00
|
|
|
{
|
2019-02-07 00:06:42 +03:00
|
|
|
time: 100,
|
|
|
|
name: "René Descartes",
|
|
|
|
email: "rene.descartes@example.com",
|
2020-01-24 02:30:13 +03:00
|
|
|
keys: []*Key{
|
2018-11-21 20:56:12 +03:00
|
|
|
{PubKey: "pubkeyA"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2019-02-07 00:06:42 +03:00
|
|
|
time: 200,
|
|
|
|
name: "René Descartes",
|
|
|
|
email: "rene.descartes@example.com",
|
2020-01-24 02:30:13 +03:00
|
|
|
keys: []*Key{
|
2018-11-21 20:56:12 +03:00
|
|
|
{PubKey: "pubkeyB"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2019-02-07 00:06:42 +03:00
|
|
|
time: 201,
|
|
|
|
name: "René Descartes",
|
|
|
|
email: "rene.descartes@example.com",
|
2020-01-24 02:30:13 +03:00
|
|
|
keys: []*Key{
|
2018-11-21 20:56:12 +03:00
|
|
|
{PubKey: "pubkeyC"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
err = identity.Commit(mockRepo)
|
|
|
|
|
2020-11-19 15:57:57 +03:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.NotEmpty(t, identity.id)
|
2018-11-21 20:56:12 +03:00
|
|
|
|
2019-02-03 21:55:35 +03:00
|
|
|
loaded, err = ReadLocal(mockRepo, identity.id)
|
2020-11-19 15:57:57 +03:00
|
|
|
require.NoError(t, err)
|
2019-01-17 04:05:50 +03:00
|
|
|
commitsAreSet(t, loaded)
|
2020-11-19 15:57:57 +03:00
|
|
|
require.Equal(t, identity, loaded)
|
2019-01-17 04:05:50 +03:00
|
|
|
|
2018-11-21 20:56:12 +03:00
|
|
|
// add more version
|
|
|
|
|
2020-01-24 02:30:13 +03:00
|
|
|
identity.addVersionForTest(&Version{
|
2019-02-07 00:06:42 +03:00
|
|
|
time: 201,
|
|
|
|
name: "René Descartes",
|
|
|
|
email: "rene.descartes@example.com",
|
2020-01-24 02:30:13 +03:00
|
|
|
keys: []*Key{
|
2018-11-21 20:56:12 +03:00
|
|
|
{PubKey: "pubkeyD"},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
2020-01-24 02:30:13 +03:00
|
|
|
identity.addVersionForTest(&Version{
|
2019-02-07 00:06:42 +03:00
|
|
|
time: 300,
|
|
|
|
name: "René Descartes",
|
|
|
|
email: "rene.descartes@example.com",
|
2020-01-24 02:30:13 +03:00
|
|
|
keys: []*Key{
|
2018-11-21 20:56:12 +03:00
|
|
|
{PubKey: "pubkeyE"},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
|
|
|
err = identity.Commit(mockRepo)
|
|
|
|
|
2020-11-19 15:57:57 +03:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.NotEmpty(t, identity.id)
|
2019-01-17 04:05:50 +03:00
|
|
|
|
2019-02-03 21:55:35 +03:00
|
|
|
loaded, err = ReadLocal(mockRepo, identity.id)
|
2020-11-19 15:57:57 +03:00
|
|
|
require.NoError(t, err)
|
2019-01-17 04:05:50 +03:00
|
|
|
commitsAreSet(t, loaded)
|
2020-11-19 15:57:57 +03:00
|
|
|
require.Equal(t, identity, loaded)
|
2019-01-17 04:05:50 +03:00
|
|
|
}
|
|
|
|
|
2020-05-01 01:25:45 +03:00
|
|
|
func TestIdentityMutate(t *testing.T) {
|
|
|
|
identity := NewIdentity("René Descartes", "rene.descartes@example.com")
|
|
|
|
|
2020-11-19 15:57:57 +03:00
|
|
|
require.Len(t, identity.versions, 1)
|
2020-05-01 01:25:45 +03:00
|
|
|
|
|
|
|
identity.Mutate(func(orig Mutator) Mutator {
|
|
|
|
orig.Email = "rene@descartes.fr"
|
|
|
|
orig.Name = "René"
|
|
|
|
orig.Login = "rene"
|
|
|
|
return orig
|
|
|
|
})
|
|
|
|
|
2020-11-19 15:57:57 +03:00
|
|
|
require.Len(t, identity.versions, 2)
|
|
|
|
require.Equal(t, identity.Email(), "rene@descartes.fr")
|
|
|
|
require.Equal(t, identity.Name(), "René")
|
|
|
|
require.Equal(t, identity.Login(), "rene")
|
2020-05-01 01:25:45 +03:00
|
|
|
}
|
|
|
|
|
2019-01-17 04:05:50 +03:00
|
|
|
func commitsAreSet(t *testing.T, identity *Identity) {
|
2019-02-07 00:06:42 +03:00
|
|
|
for _, version := range identity.versions {
|
2020-11-19 15:57:57 +03:00
|
|
|
require.NotEmpty(t, version.commitHash)
|
2019-01-17 04:05:50 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-20 17:41:27 +03:00
|
|
|
// Test that the correct crypto keys are returned for a given lamport time
|
2018-11-21 20:56:12 +03:00
|
|
|
func TestIdentity_ValidKeysAtTime(t *testing.T) {
|
|
|
|
identity := Identity{
|
2019-08-11 15:08:03 +03:00
|
|
|
id: entity.UnsetId,
|
2019-02-07 00:06:42 +03:00
|
|
|
versions: []*Version{
|
2018-11-21 20:56:12 +03:00
|
|
|
{
|
2019-02-07 00:06:42 +03:00
|
|
|
time: 100,
|
|
|
|
name: "René Descartes",
|
|
|
|
email: "rene.descartes@example.com",
|
2020-01-24 02:30:13 +03:00
|
|
|
keys: []*Key{
|
2018-11-21 20:56:12 +03:00
|
|
|
{PubKey: "pubkeyA"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2019-02-07 00:06:42 +03:00
|
|
|
time: 200,
|
|
|
|
name: "René Descartes",
|
|
|
|
email: "rene.descartes@example.com",
|
2020-01-24 02:30:13 +03:00
|
|
|
keys: []*Key{
|
2018-11-21 20:56:12 +03:00
|
|
|
{PubKey: "pubkeyB"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2019-02-07 00:06:42 +03:00
|
|
|
time: 201,
|
|
|
|
name: "René Descartes",
|
|
|
|
email: "rene.descartes@example.com",
|
2020-01-24 02:30:13 +03:00
|
|
|
keys: []*Key{
|
2018-11-21 20:56:12 +03:00
|
|
|
{PubKey: "pubkeyC"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2019-02-07 00:06:42 +03:00
|
|
|
time: 201,
|
|
|
|
name: "René Descartes",
|
|
|
|
email: "rene.descartes@example.com",
|
2020-01-24 02:30:13 +03:00
|
|
|
keys: []*Key{
|
2018-11-21 20:56:12 +03:00
|
|
|
{PubKey: "pubkeyD"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2019-02-07 00:06:42 +03:00
|
|
|
time: 300,
|
|
|
|
name: "René Descartes",
|
|
|
|
email: "rene.descartes@example.com",
|
2020-01-24 02:30:13 +03:00
|
|
|
keys: []*Key{
|
2018-11-21 20:56:12 +03:00
|
|
|
{PubKey: "pubkeyE"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2020-11-19 15:57:57 +03:00
|
|
|
require.Nil(t, identity.ValidKeysAtTime(10))
|
|
|
|
require.Equal(t, identity.ValidKeysAtTime(100), []*Key{{PubKey: "pubkeyA"}})
|
|
|
|
require.Equal(t, identity.ValidKeysAtTime(140), []*Key{{PubKey: "pubkeyA"}})
|
|
|
|
require.Equal(t, identity.ValidKeysAtTime(200), []*Key{{PubKey: "pubkeyB"}})
|
|
|
|
require.Equal(t, identity.ValidKeysAtTime(201), []*Key{{PubKey: "pubkeyD"}})
|
|
|
|
require.Equal(t, identity.ValidKeysAtTime(202), []*Key{{PubKey: "pubkeyD"}})
|
|
|
|
require.Equal(t, identity.ValidKeysAtTime(300), []*Key{{PubKey: "pubkeyE"}})
|
|
|
|
require.Equal(t, identity.ValidKeysAtTime(3000), []*Key{{PubKey: "pubkeyE"}})
|
2018-11-21 20:56:12 +03:00
|
|
|
}
|
2019-01-17 04:05:50 +03:00
|
|
|
|
2019-01-20 17:41:27 +03:00
|
|
|
// Test the immutable or mutable metadata search
|
2019-01-17 04:05:50 +03:00
|
|
|
func TestMetadata(t *testing.T) {
|
2020-11-08 19:54:28 +03:00
|
|
|
mockRepo := repository.NewMockRepo()
|
2019-01-17 04:05:50 +03:00
|
|
|
|
|
|
|
identity := NewIdentity("René Descartes", "rene.descartes@example.com")
|
|
|
|
|
|
|
|
identity.SetMetadata("key1", "value1")
|
|
|
|
assertHasKeyValue(t, identity.ImmutableMetadata(), "key1", "value1")
|
|
|
|
assertHasKeyValue(t, identity.MutableMetadata(), "key1", "value1")
|
|
|
|
|
|
|
|
err := identity.Commit(mockRepo)
|
2020-11-19 15:57:57 +03:00
|
|
|
require.NoError(t, err)
|
2019-01-17 04:05:50 +03:00
|
|
|
|
|
|
|
assertHasKeyValue(t, identity.ImmutableMetadata(), "key1", "value1")
|
|
|
|
assertHasKeyValue(t, identity.MutableMetadata(), "key1", "value1")
|
|
|
|
|
|
|
|
// try override
|
2020-01-24 02:30:13 +03:00
|
|
|
identity.addVersionForTest(&Version{
|
2019-02-07 00:06:42 +03:00
|
|
|
name: "René Descartes",
|
|
|
|
email: "rene.descartes@example.com",
|
2019-01-17 04:05:50 +03:00
|
|
|
})
|
|
|
|
|
|
|
|
identity.SetMetadata("key1", "value2")
|
|
|
|
assertHasKeyValue(t, identity.ImmutableMetadata(), "key1", "value1")
|
|
|
|
assertHasKeyValue(t, identity.MutableMetadata(), "key1", "value2")
|
|
|
|
|
|
|
|
err = identity.Commit(mockRepo)
|
2020-11-19 15:57:57 +03:00
|
|
|
require.NoError(t, err)
|
2019-01-17 04:05:50 +03:00
|
|
|
|
|
|
|
// reload
|
2019-02-03 21:55:35 +03:00
|
|
|
loaded, err := ReadLocal(mockRepo, identity.id)
|
2020-11-19 15:57:57 +03:00
|
|
|
require.NoError(t, err)
|
2019-01-17 04:05:50 +03:00
|
|
|
|
|
|
|
assertHasKeyValue(t, loaded.ImmutableMetadata(), "key1", "value1")
|
|
|
|
assertHasKeyValue(t, loaded.MutableMetadata(), "key1", "value2")
|
|
|
|
}
|
|
|
|
|
|
|
|
func assertHasKeyValue(t *testing.T, metadata map[string]string, key, value string) {
|
|
|
|
val, ok := metadata[key]
|
2020-11-19 15:57:57 +03:00
|
|
|
require.True(t, ok)
|
|
|
|
require.Equal(t, val, value)
|
2019-01-17 04:05:50 +03:00
|
|
|
}
|
2019-02-01 14:22:00 +03:00
|
|
|
|
|
|
|
func TestJSON(t *testing.T) {
|
2020-11-08 19:54:28 +03:00
|
|
|
mockRepo := repository.NewMockRepo()
|
2019-02-01 14:22:00 +03:00
|
|
|
|
|
|
|
identity := &Identity{
|
2019-08-11 15:08:03 +03:00
|
|
|
id: entity.UnsetId,
|
2019-02-07 00:06:42 +03:00
|
|
|
versions: []*Version{
|
2019-02-01 14:22:00 +03:00
|
|
|
{
|
2019-02-07 00:06:42 +03:00
|
|
|
name: "René Descartes",
|
|
|
|
email: "rene.descartes@example.com",
|
2019-02-01 14:22:00 +03:00
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2019-08-11 15:08:03 +03:00
|
|
|
// commit to make sure we have an Id
|
2019-02-01 14:22:00 +03:00
|
|
|
err := identity.Commit(mockRepo)
|
2020-11-19 15:57:57 +03:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.NotEmpty(t, identity.id)
|
2019-02-01 14:22:00 +03:00
|
|
|
|
|
|
|
// serialize
|
|
|
|
data, err := json.Marshal(identity)
|
2020-11-19 15:57:57 +03:00
|
|
|
require.NoError(t, err)
|
2019-02-01 14:22:00 +03:00
|
|
|
|
|
|
|
// deserialize, got a IdentityStub with the same id
|
|
|
|
var i Interface
|
|
|
|
i, err = UnmarshalJSON(data)
|
2020-11-19 15:57:57 +03:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, identity.id, i.Id())
|
2019-02-01 14:22:00 +03:00
|
|
|
|
|
|
|
// make sure we can load the identity properly
|
2019-02-03 21:55:35 +03:00
|
|
|
i, err = ReadLocal(mockRepo, i.Id())
|
2020-11-19 15:57:57 +03:00
|
|
|
require.NoError(t, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestIdentityRemove(t *testing.T) {
|
|
|
|
repo := repository.CreateGoGitTestRepo(false)
|
|
|
|
remoteA := repository.CreateGoGitTestRepo(true)
|
|
|
|
remoteB := repository.CreateGoGitTestRepo(true)
|
|
|
|
defer repository.CleanupTestRepos(repo, remoteA, remoteB)
|
|
|
|
|
2020-12-08 16:42:13 +03:00
|
|
|
err := repo.AddRemote("remoteA", remoteA.GetLocalRemote())
|
2020-11-19 15:57:57 +03:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2020-12-08 16:42:13 +03:00
|
|
|
err = repo.AddRemote("remoteB", remoteB.GetLocalRemote())
|
2020-11-19 15:57:57 +03:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
// generate an identity for testing
|
|
|
|
rene := NewIdentity("René Descartes", "rene@descartes.fr")
|
|
|
|
err = rene.Commit(repo)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
_, err = Push(repo, "remoteA")
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
_, err = Push(repo, "remoteB")
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
_, err = Fetch(repo, "remoteA")
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
_, err = Fetch(repo, "remoteB")
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
err = RemoveIdentity(repo, rene.Id())
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
_, err = ReadLocal(repo, rene.Id())
|
|
|
|
require.Error(t, ErrIdentityNotExist, err)
|
|
|
|
|
|
|
|
_, err = ReadRemote(repo, "remoteA", string(rene.Id()))
|
|
|
|
require.Error(t, ErrIdentityNotExist, err)
|
|
|
|
|
|
|
|
_, err = ReadRemote(repo, "remoteB", string(rene.Id()))
|
|
|
|
require.Error(t, ErrIdentityNotExist, err)
|
|
|
|
|
|
|
|
ids, err := ListLocalIds(repo)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Len(t, ids, 0)
|
2019-02-01 14:22:00 +03:00
|
|
|
}
|