aboutsummaryrefslogtreecommitdiffstats
path: root/identity/identity_test.go
blob: ee6ccdf7dfd2140bbedb31a276137f51630a970a (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
package identity

import (
	"encoding/json"
	"testing"

	"github.com/MichaelMure/git-bug/entity"
	"github.com/MichaelMure/git-bug/repository"
	"github.com/stretchr/testify/assert"
)

// Test the commit and load of an Identity with multiple versions
func TestIdentityCommitLoad(t *testing.T) {
	mockRepo := repository.NewMockRepoForTest()

	// single version

	identity := &Identity{
		id: entity.UnsetId,
		versions: []*Version{
			{
				name:  "René Descartes",
				email: "rene.descartes@example.com",
			},
		},
	}

	err := identity.Commit(mockRepo)

	assert.Nil(t, err)
	assert.NotEmpty(t, identity.id)

	loaded, err := ReadLocal(mockRepo, identity.id)
	assert.Nil(t, err)
	commitsAreSet(t, loaded)
	assert.Equal(t, identity, loaded)

	// multiple version

	identity = &Identity{
		id: entity.UnsetId,
		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)

	loaded, err = ReadLocal(mockRepo, identity.id)
	assert.Nil(t, err)
	commitsAreSet(t, loaded)
	assert.Equal(t, identity, loaded)

	// add more version

	identity.Mutate(func(orig Mutator) Mutator {

		return orig
	})

	identity.addVersionForTest(&Version{
		time:  201,
		name:  "René Descartes",
		email: "rene.descartes@example.com",
		keys: []*Key{
			{PubKey: "pubkeyD"},
		},
	})

	identity.addVersionForTest(&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)

	loaded, err = ReadLocal(mockRepo, identity.id)
	assert.Nil(t, err)
	commitsAreSet(t, loaded)
	assert.Equal(t, identity, loaded)
}

func commitsAreSet(t *testing.T, identity *Identity) {
	for _, version := range identity.versions {
		assert.NotEmpty(t, version.commitHash)
	}
}

// Test that the correct crypto keys are returned for a given lamport time
func TestIdentity_ValidKeysAtTime(t *testing.T) {
	identity := Identity{
		id: entity.UnsetId,
		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"}})
}

// Test the immutable or mutable metadata search
func TestMetadata(t *testing.T) {
	mockRepo := repository.NewMockRepoForTest()

	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)
	assert.NoError(t, err)

	assertHasKeyValue(t, identity.ImmutableMetadata(), "key1", "value1")
	assertHasKeyValue(t, identity.MutableMetadata(), "key1", "value1")

	// try override
	identity.addVersionForTest(&Version{
		name:  "René Descartes",
		email: "rene.descartes@example.com",
	})

	identity.SetMetadata("key1", "value2")
	assertHasKeyValue(t, identity.ImmutableMetadata(), "key1", "value1")
	assertHasKeyValue(t, identity.MutableMetadata(), "key1", "value2")

	err = identity.Commit(mockRepo)
	assert.NoError(t, err)

	// reload
	loaded, err := ReadLocal(mockRepo, identity.id)
	assert.Nil(t, err)

	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]
	assert.True(t, ok)
	assert.Equal(t, val, value)
}

func TestJSON(t *testing.T) {
	mockRepo := repository.NewMockRepoForTest()

	identity := &Identity{
		id: entity.UnsetId,
		versions: []*Version{
			{
				name:  "René Descartes",
				email: "rene.descartes@example.com",
			},
		},
	}

	// commit to make sure we have an Id
	err := identity.Commit(mockRepo)
	assert.Nil(t, err)
	assert.NotEmpty(t, identity.id)

	// serialize
	data, err := json.Marshal(identity)
	assert.NoError(t, err)

	// deserialize, got a IdentityStub with the same id
	var i Interface
	i, err = UnmarshalJSON(data)
	assert.NoError(t, err)
	assert.Equal(t, identity.id, i.Id())

	// make sure we can load the identity properly
	i, err = ReadLocal(mockRepo, i.Id())
	assert.NoError(t, err)
}