diff options
Diffstat (limited to 'identity/identity_test.go')
-rw-r--r-- | identity/identity_test.go | 145 |
1 files changed, 145 insertions, 0 deletions
diff --git a/identity/identity_test.go b/identity/identity_test.go new file mode 100644 index 00000000..161fd56f --- /dev/null +++ b/identity/identity_test.go @@ -0,0 +1,145 @@ +package identity + +import ( + "testing" + + "github.com/MichaelMure/git-bug/repository" + "github.com/stretchr/testify/assert" +) + +func TestIdentityCommit(t *testing.T) { + mockRepo := repository.NewMockRepoForTest() + + // single version + + identity := Identity{ + Versions: []Version{ + { + Name: "René Descartes", + Email: "rene.descartes@example.com", + }, + }, + } + + err := identity.Commit(mockRepo) + + assert.Nil(t, err) + assert.NotEmpty(t, identity.id) + + // multiple version + + identity = Identity{ + Versions: []Version{ + { + Time: 100, + Name: "René Descartes", + Email: "rene.descartes@example.com", + Keys: []Key{ + {PubKey: "pubkeyA"}, + }, + }, + { + Time: 200, + Name: "René Descartes", + Email: "rene.descartes@example.com", + Keys: []Key{ + {PubKey: "pubkeyB"}, + }, + }, + { + Time: 201, + Name: "René Descartes", + Email: "rene.descartes@example.com", + Keys: []Key{ + {PubKey: "pubkeyC"}, + }, + }, + }, + } + + err = identity.Commit(mockRepo) + + assert.Nil(t, err) + assert.NotEmpty(t, identity.id) + + // add more version + + identity.AddVersion(Version{ + Time: 201, + Name: "René Descartes", + Email: "rene.descartes@example.com", + Keys: []Key{ + {PubKey: "pubkeyD"}, + }, + }) + + identity.AddVersion(Version{ + Time: 300, + Name: "René Descartes", + Email: "rene.descartes@example.com", + Keys: []Key{ + {PubKey: "pubkeyE"}, + }, + }) + + err = identity.Commit(mockRepo) + + assert.Nil(t, err) + assert.NotEmpty(t, identity.id) +} + +func TestIdentity_ValidKeysAtTime(t *testing.T) { + identity := Identity{ + Versions: []Version{ + { + Time: 100, + Name: "René Descartes", + Email: "rene.descartes@example.com", + Keys: []Key{ + {PubKey: "pubkeyA"}, + }, + }, + { + Time: 200, + Name: "René Descartes", + Email: "rene.descartes@example.com", + Keys: []Key{ + {PubKey: "pubkeyB"}, + }, + }, + { + Time: 201, + Name: "René Descartes", + Email: "rene.descartes@example.com", + Keys: []Key{ + {PubKey: "pubkeyC"}, + }, + }, + { + Time: 201, + Name: "René Descartes", + Email: "rene.descartes@example.com", + Keys: []Key{ + {PubKey: "pubkeyD"}, + }, + }, + { + Time: 300, + Name: "René Descartes", + Email: "rene.descartes@example.com", + Keys: []Key{ + {PubKey: "pubkeyE"}, + }, + }, + }, + } + + assert.Nil(t, identity.ValidKeysAtTime(10)) + assert.Equal(t, identity.ValidKeysAtTime(100), []Key{{PubKey: "pubkeyA"}}) + assert.Equal(t, identity.ValidKeysAtTime(140), []Key{{PubKey: "pubkeyA"}}) + assert.Equal(t, identity.ValidKeysAtTime(200), []Key{{PubKey: "pubkeyB"}}) + assert.Equal(t, identity.ValidKeysAtTime(201), []Key{{PubKey: "pubkeyD"}}) + assert.Equal(t, identity.ValidKeysAtTime(202), []Key{{PubKey: "pubkeyD"}}) + assert.Equal(t, identity.ValidKeysAtTime(300), []Key{{PubKey: "pubkeyE"}}) + assert.Equal(t, identity.ValidKeysAtTime(3000), []Key{{PubKey: "pubkeyE"}}) +} |