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"
|
|
|
|
|
2018-11-21 20:56:12 +03:00
|
|
|
"github.com/MichaelMure/git-bug/repository"
|
2020-11-08 21:13:55 +03:00
|
|
|
"github.com/MichaelMure/git-bug/util/lamport"
|
2018-11-21 20:56:12 +03:00
|
|
|
)
|
|
|
|
|
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 21:13:55 +03:00
|
|
|
repo := makeIdentityTestRepo(t)
|
2018-11-21 20:56:12 +03:00
|
|
|
|
|
|
|
// single version
|
|
|
|
|
2020-11-08 21:13:55 +03:00
|
|
|
identity, err := NewIdentity(repo, "René Descartes", "rene.descartes@example.com")
|
|
|
|
require.NoError(t, err)
|
2018-11-21 20:56:12 +03:00
|
|
|
|
2020-11-08 21:13:55 +03:00
|
|
|
idBeforeCommit := identity.Id()
|
2018-11-21 20:56:12 +03:00
|
|
|
|
2020-11-08 21:13:55 +03:00
|
|
|
err = identity.Commit(repo)
|
2020-11-19 15:57:57 +03:00
|
|
|
require.NoError(t, err)
|
2018-11-21 20:56:12 +03:00
|
|
|
|
2020-11-08 21:13:55 +03:00
|
|
|
commitsAreSet(t, identity)
|
|
|
|
require.NotEmpty(t, identity.Id())
|
|
|
|
require.Equal(t, idBeforeCommit, identity.Id())
|
|
|
|
require.Equal(t, idBeforeCommit, identity.versions[0].Id())
|
|
|
|
|
|
|
|
loaded, err := ReadLocal(repo, 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-11-08 21:13:55 +03:00
|
|
|
// multiple versions
|
2018-11-21 20:56:12 +03:00
|
|
|
|
2021-01-04 01:59:25 +03:00
|
|
|
identity, err = NewIdentityFull(repo, "René Descartes", "rene.descartes@example.com", "", "", []*Key{generatePublicKey()})
|
2020-11-08 21:13:55 +03:00
|
|
|
require.NoError(t, err)
|
2018-11-21 20:56:12 +03:00
|
|
|
|
2020-11-08 21:13:55 +03:00
|
|
|
idBeforeCommit = identity.Id()
|
2018-11-21 20:56:12 +03:00
|
|
|
|
2020-11-08 21:13:55 +03:00
|
|
|
err = identity.Mutate(repo, func(orig *Mutator) {
|
2021-01-04 01:59:25 +03:00
|
|
|
orig.Keys = []*Key{generatePublicKey()}
|
2020-11-08 21:13:55 +03:00
|
|
|
})
|
2020-11-19 15:57:57 +03:00
|
|
|
require.NoError(t, err)
|
2018-11-21 20:56:12 +03:00
|
|
|
|
2020-11-08 21:13:55 +03:00
|
|
|
err = identity.Mutate(repo, func(orig *Mutator) {
|
2021-01-04 01:59:25 +03:00
|
|
|
orig.Keys = []*Key{generatePublicKey()}
|
2020-11-08 21:13:55 +03:00
|
|
|
})
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
require.Equal(t, idBeforeCommit, identity.Id())
|
|
|
|
|
|
|
|
err = identity.Commit(repo)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
commitsAreSet(t, identity)
|
|
|
|
require.NotEmpty(t, identity.Id())
|
|
|
|
require.Equal(t, idBeforeCommit, identity.Id())
|
|
|
|
require.Equal(t, idBeforeCommit, identity.versions[0].Id())
|
|
|
|
|
|
|
|
loaded, err = ReadLocal(repo, 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-11-08 21:13:55 +03:00
|
|
|
err = identity.Mutate(repo, func(orig *Mutator) {
|
|
|
|
orig.Email = "rene@descartes.com"
|
2021-01-04 01:59:25 +03:00
|
|
|
orig.Keys = []*Key{generatePublicKey()}
|
2018-11-21 20:56:12 +03:00
|
|
|
})
|
2020-11-08 21:13:55 +03:00
|
|
|
require.NoError(t, err)
|
2018-11-21 20:56:12 +03:00
|
|
|
|
2020-11-08 21:13:55 +03:00
|
|
|
err = identity.Mutate(repo, func(orig *Mutator) {
|
|
|
|
orig.Email = "rene@descartes.com"
|
2021-01-04 01:59:25 +03:00
|
|
|
orig.Keys = []*Key{generatePublicKey(), generatePublicKey()}
|
2018-11-21 20:56:12 +03:00
|
|
|
})
|
2020-11-08 21:13:55 +03:00
|
|
|
require.NoError(t, err)
|
2018-11-21 20:56:12 +03:00
|
|
|
|
2020-11-08 21:13:55 +03:00
|
|
|
err = identity.Commit(repo)
|
2020-11-19 15:57:57 +03:00
|
|
|
require.NoError(t, err)
|
2019-01-17 04:05:50 +03:00
|
|
|
|
2020-11-08 21:13:55 +03:00
|
|
|
commitsAreSet(t, identity)
|
|
|
|
require.NotEmpty(t, identity.Id())
|
|
|
|
require.Equal(t, idBeforeCommit, identity.Id())
|
|
|
|
require.Equal(t, idBeforeCommit, identity.versions[0].Id())
|
|
|
|
|
|
|
|
loaded, err = ReadLocal(repo, 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) {
|
2020-11-08 21:13:55 +03:00
|
|
|
repo := makeIdentityTestRepo(t)
|
|
|
|
|
|
|
|
identity, err := NewIdentity(repo, "René Descartes", "rene.descartes@example.com")
|
|
|
|
require.NoError(t, err)
|
2020-05-01 01:25:45 +03:00
|
|
|
|
2020-11-19 15:57:57 +03:00
|
|
|
require.Len(t, identity.versions, 1)
|
2020-05-01 01:25:45 +03:00
|
|
|
|
2020-11-08 21:13:55 +03:00
|
|
|
err = identity.Mutate(repo, func(orig *Mutator) {
|
2020-05-01 01:25:45 +03:00
|
|
|
orig.Email = "rene@descartes.fr"
|
|
|
|
orig.Name = "René"
|
|
|
|
orig.Login = "rene"
|
|
|
|
})
|
2020-11-08 21:13:55 +03:00
|
|
|
require.NoError(t, err)
|
2020-05-01 01:25:45 +03:00
|
|
|
|
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) {
|
2021-01-04 01:59:25 +03:00
|
|
|
pubKeyA := generatePublicKey()
|
|
|
|
pubKeyB := generatePublicKey()
|
|
|
|
pubKeyC := generatePublicKey()
|
|
|
|
pubKeyD := generatePublicKey()
|
|
|
|
pubKeyE := generatePublicKey()
|
|
|
|
|
2018-11-21 20:56:12 +03:00
|
|
|
identity := Identity{
|
2020-11-08 21:13:55 +03:00
|
|
|
versions: []*version{
|
2018-11-21 20:56:12 +03:00
|
|
|
{
|
2020-11-08 21:13:55 +03:00
|
|
|
times: map[string]lamport.Time{"foo": 100},
|
2021-01-04 01:59:25 +03:00
|
|
|
keys: []*Key{pubKeyA},
|
2018-11-21 20:56:12 +03:00
|
|
|
},
|
|
|
|
{
|
2020-11-08 21:13:55 +03:00
|
|
|
times: map[string]lamport.Time{"foo": 200},
|
2021-01-04 01:59:25 +03:00
|
|
|
keys: []*Key{pubKeyB},
|
2018-11-21 20:56:12 +03:00
|
|
|
},
|
|
|
|
{
|
2020-11-08 21:13:55 +03:00
|
|
|
times: map[string]lamport.Time{"foo": 201},
|
2021-01-04 01:59:25 +03:00
|
|
|
keys: []*Key{pubKeyC},
|
2018-11-21 20:56:12 +03:00
|
|
|
},
|
|
|
|
{
|
2020-11-08 21:13:55 +03:00
|
|
|
times: map[string]lamport.Time{"foo": 201},
|
2021-01-04 01:59:25 +03:00
|
|
|
keys: []*Key{pubKeyD},
|
2018-11-21 20:56:12 +03:00
|
|
|
},
|
|
|
|
{
|
2020-11-08 21:13:55 +03:00
|
|
|
times: map[string]lamport.Time{"foo": 300},
|
2021-01-04 01:59:25 +03:00
|
|
|
keys: []*Key{pubKeyE},
|
2018-11-21 20:56:12 +03:00
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2020-11-08 21:13:55 +03:00
|
|
|
require.Nil(t, identity.ValidKeysAtTime("foo", 10))
|
2021-01-04 01:59:25 +03:00
|
|
|
require.Equal(t, identity.ValidKeysAtTime("foo", 100), []*Key{pubKeyA})
|
|
|
|
require.Equal(t, identity.ValidKeysAtTime("foo", 140), []*Key{pubKeyA})
|
|
|
|
require.Equal(t, identity.ValidKeysAtTime("foo", 200), []*Key{pubKeyB})
|
|
|
|
require.Equal(t, identity.ValidKeysAtTime("foo", 201), []*Key{pubKeyD})
|
|
|
|
require.Equal(t, identity.ValidKeysAtTime("foo", 202), []*Key{pubKeyD})
|
|
|
|
require.Equal(t, identity.ValidKeysAtTime("foo", 300), []*Key{pubKeyE})
|
|
|
|
require.Equal(t, identity.ValidKeysAtTime("foo", 3000), []*Key{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 21:13:55 +03:00
|
|
|
repo := makeIdentityTestRepo(t)
|
2019-01-17 04:05:50 +03:00
|
|
|
|
2020-11-08 21:13:55 +03:00
|
|
|
identity, err := NewIdentity(repo, "René Descartes", "rene.descartes@example.com")
|
|
|
|
require.NoError(t, err)
|
2019-01-17 04:05:50 +03:00
|
|
|
|
|
|
|
identity.SetMetadata("key1", "value1")
|
|
|
|
assertHasKeyValue(t, identity.ImmutableMetadata(), "key1", "value1")
|
|
|
|
assertHasKeyValue(t, identity.MutableMetadata(), "key1", "value1")
|
|
|
|
|
2020-11-08 21:13:55 +03:00
|
|
|
err = identity.Commit(repo)
|
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-11-08 21:13:55 +03:00
|
|
|
err = identity.Mutate(repo, func(orig *Mutator) {
|
|
|
|
orig.Email = "rene@descartes.fr"
|
2019-01-17 04:05:50 +03:00
|
|
|
})
|
2020-11-08 21:13:55 +03:00
|
|
|
require.NoError(t, err)
|
2019-01-17 04:05:50 +03:00
|
|
|
|
|
|
|
identity.SetMetadata("key1", "value2")
|
|
|
|
assertHasKeyValue(t, identity.ImmutableMetadata(), "key1", "value1")
|
|
|
|
assertHasKeyValue(t, identity.MutableMetadata(), "key1", "value2")
|
|
|
|
|
2020-11-08 21:13:55 +03:00
|
|
|
err = identity.Commit(repo)
|
2020-11-19 15:57:57 +03:00
|
|
|
require.NoError(t, err)
|
2019-01-17 04:05:50 +03:00
|
|
|
|
|
|
|
// reload
|
2020-11-08 21:13:55 +03:00
|
|
|
loaded, err := ReadLocal(repo, 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")
|
2020-11-09 01:56:32 +03:00
|
|
|
|
|
|
|
// set metadata after commit
|
|
|
|
versionCount := len(identity.versions)
|
|
|
|
identity.SetMetadata("foo", "bar")
|
|
|
|
require.True(t, identity.NeedCommit())
|
|
|
|
require.Len(t, identity.versions, versionCount+1)
|
|
|
|
|
|
|
|
err = identity.Commit(repo)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Len(t, identity.versions, versionCount+1)
|
2019-01-17 04:05:50 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
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 21:13:55 +03:00
|
|
|
repo := makeIdentityTestRepo(t)
|
2019-02-01 14:22:00 +03:00
|
|
|
|
2020-11-08 21:13:55 +03:00
|
|
|
identity, err := NewIdentity(repo, "René Descartes", "rene.descartes@example.com")
|
|
|
|
require.NoError(t, err)
|
2019-02-01 14:22:00 +03:00
|
|
|
|
2019-08-11 15:08:03 +03:00
|
|
|
// commit to make sure we have an Id
|
2020-11-08 21:13:55 +03:00
|
|
|
err = identity.Commit(repo)
|
2020-11-19 15:57:57 +03:00
|
|
|
require.NoError(t, err)
|
2020-11-08 21:13:55 +03:00
|
|
|
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)
|
2020-11-08 21:13:55 +03:00
|
|
|
require.Equal(t, identity.Id(), i.Id())
|
2019-02-01 14:22:00 +03:00
|
|
|
|
|
|
|
// make sure we can load the identity properly
|
2020-11-08 21:13:55 +03:00
|
|
|
i, err = ReadLocal(repo, i.Id())
|
2020-11-19 15:57:57 +03:00
|
|
|
require.NoError(t, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestIdentityRemove(t *testing.T) {
|
2022-06-16 16:02:52 +03:00
|
|
|
repo := repository.CreateGoGitTestRepo(t, false)
|
|
|
|
remoteA := repository.CreateGoGitTestRepo(t, true)
|
|
|
|
remoteB := repository.CreateGoGitTestRepo(t, true)
|
2020-11-19 15:57:57 +03:00
|
|
|
|
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
|
2020-11-08 21:13:55 +03:00
|
|
|
rene, err := NewIdentity(repo, "René Descartes", "rene@descartes.fr")
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
2020-11-19 15:57:57 +03:00
|
|
|
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
|
|
|
}
|