add constructor functions with options. update tests.
This commit is contained in:
+133
-140
@@ -14,169 +14,161 @@ type ValidateEventTestCase struct {
|
||||
var structureTestCases = []ValidateEventTestCase{
|
||||
{
|
||||
name: "empty pubkey",
|
||||
event: Event{
|
||||
ID: testEvent.ID,
|
||||
PubKey: "",
|
||||
CreatedAt: testEvent.CreatedAt,
|
||||
Kind: testEvent.Kind,
|
||||
Tags: testEvent.Tags,
|
||||
Content: testEvent.Content,
|
||||
Sig: testEvent.Sig,
|
||||
},
|
||||
event: NewEvent(
|
||||
WithID(testEvent.ID),
|
||||
WithPubKey(""),
|
||||
WithCreatedAt(testEvent.CreatedAt),
|
||||
WithKind(testEvent.Kind),
|
||||
WithContent(testEvent.Content),
|
||||
WithSig(testEvent.Sig),
|
||||
),
|
||||
expectedError: "public key must be 64 lowercase hex characters",
|
||||
},
|
||||
|
||||
{
|
||||
name: "short pubkey",
|
||||
event: Event{
|
||||
ID: testEvent.ID,
|
||||
PubKey: "abc123",
|
||||
CreatedAt: testEvent.CreatedAt,
|
||||
Kind: testEvent.Kind,
|
||||
Tags: testEvent.Tags,
|
||||
Content: testEvent.Content,
|
||||
Sig: testEvent.Sig,
|
||||
},
|
||||
event: NewEvent(
|
||||
WithID(testEvent.ID),
|
||||
WithPubKey("abc123"),
|
||||
WithCreatedAt(testEvent.CreatedAt),
|
||||
WithKind(testEvent.Kind),
|
||||
WithContent(testEvent.Content),
|
||||
WithSig(testEvent.Sig),
|
||||
),
|
||||
expectedError: "public key must be 64 lowercase hex characters",
|
||||
},
|
||||
|
||||
{
|
||||
name: "long pubkey",
|
||||
event: Event{
|
||||
ID: testEvent.ID,
|
||||
PubKey: "c7a702e6158744ca03508bbb4c90f9dbb0d6e88fefbfaa511d5ab24b4e3c48adabc",
|
||||
CreatedAt: testEvent.CreatedAt,
|
||||
Kind: testEvent.Kind,
|
||||
Tags: testEvent.Tags,
|
||||
Content: testEvent.Content,
|
||||
Sig: testEvent.Sig,
|
||||
},
|
||||
event: NewEvent(
|
||||
WithID(testEvent.ID),
|
||||
WithPubKey("c7a702e6158744ca03508bbb4c90f9dbb0d6e88fefbfaa511d5ab24b4e3c48adabc"),
|
||||
WithCreatedAt(testEvent.CreatedAt),
|
||||
WithKind(testEvent.Kind),
|
||||
WithContent(testEvent.Content),
|
||||
WithSig(testEvent.Sig),
|
||||
),
|
||||
expectedError: "public key must be 64 lowercase hex characters",
|
||||
},
|
||||
|
||||
{
|
||||
name: "non-hex pubkey",
|
||||
event: Event{
|
||||
ID: testEvent.ID,
|
||||
PubKey: "zyx-!2e6158744ca03508bbb4c90f9dbb0d6e88fefbfaa511d5ab24b4e3c48ad",
|
||||
CreatedAt: testEvent.CreatedAt,
|
||||
Kind: testEvent.Kind,
|
||||
Tags: testEvent.Tags,
|
||||
Content: testEvent.Content,
|
||||
Sig: testEvent.Sig,
|
||||
},
|
||||
event: NewEvent(
|
||||
WithID(testEvent.ID),
|
||||
WithPubKey("zyx-!2e6158744ca03508bbb4c90f9dbb0d6e88fefbfaa511d5ab24b4e3c48ad"),
|
||||
WithCreatedAt(testEvent.CreatedAt),
|
||||
WithKind(testEvent.Kind),
|
||||
WithContent(testEvent.Content),
|
||||
WithSig(testEvent.Sig),
|
||||
),
|
||||
expectedError: "public key must be 64 lowercase hex characters",
|
||||
},
|
||||
|
||||
{
|
||||
name: "uppercase pubkey",
|
||||
event: Event{
|
||||
ID: testEvent.ID,
|
||||
PubKey: "C7A702E6158744CA03508BBB4C90F9DBB0D6E88FEFBFAA511D5AB24B4E3C48AD",
|
||||
CreatedAt: testEvent.CreatedAt,
|
||||
Kind: testEvent.Kind,
|
||||
Tags: testEvent.Tags,
|
||||
Content: testEvent.Content,
|
||||
Sig: testEvent.Sig,
|
||||
},
|
||||
event: NewEvent(
|
||||
WithID(testEvent.ID),
|
||||
WithPubKey("C7A702E6158744CA03508BBB4C90F9DBB0D6E88FEFBFAA511D5AB24B4E3C48AD"),
|
||||
WithCreatedAt(testEvent.CreatedAt),
|
||||
WithKind(testEvent.Kind),
|
||||
WithContent(testEvent.Content),
|
||||
WithSig(testEvent.Sig),
|
||||
),
|
||||
expectedError: "public key must be 64 lowercase hex characters",
|
||||
},
|
||||
|
||||
{
|
||||
name: "empty id",
|
||||
event: Event{
|
||||
ID: "",
|
||||
PubKey: testEvent.PubKey,
|
||||
CreatedAt: testEvent.CreatedAt,
|
||||
Kind: testEvent.Kind,
|
||||
Tags: testEvent.Tags,
|
||||
Content: testEvent.Content,
|
||||
Sig: testEvent.Sig,
|
||||
},
|
||||
event: NewEvent(
|
||||
WithID(""),
|
||||
WithPubKey(testEvent.PubKey),
|
||||
WithCreatedAt(testEvent.CreatedAt),
|
||||
WithKind(testEvent.Kind),
|
||||
WithContent(testEvent.Content),
|
||||
WithSig(testEvent.Sig),
|
||||
),
|
||||
expectedError: "id must be 64 hex characters",
|
||||
},
|
||||
|
||||
{
|
||||
name: "short id",
|
||||
event: Event{
|
||||
ID: "abc123",
|
||||
PubKey: testEvent.PubKey,
|
||||
CreatedAt: testEvent.CreatedAt,
|
||||
Kind: testEvent.Kind,
|
||||
Tags: testEvent.Tags,
|
||||
Content: testEvent.Content,
|
||||
Sig: testEvent.Sig,
|
||||
},
|
||||
event: NewEvent(
|
||||
WithID("abc123"),
|
||||
WithPubKey(testEvent.PubKey),
|
||||
WithCreatedAt(testEvent.CreatedAt),
|
||||
WithKind(testEvent.Kind),
|
||||
WithContent(testEvent.Content),
|
||||
WithSig(testEvent.Sig),
|
||||
),
|
||||
expectedError: "id must be 64 hex characters",
|
||||
},
|
||||
|
||||
{
|
||||
name: "empty signature",
|
||||
event: Event{
|
||||
ID: testEvent.ID,
|
||||
PubKey: testEvent.PubKey,
|
||||
CreatedAt: testEvent.CreatedAt,
|
||||
Kind: testEvent.Kind,
|
||||
Tags: testEvent.Tags,
|
||||
Content: testEvent.Content,
|
||||
Sig: "",
|
||||
},
|
||||
event: NewEvent(
|
||||
WithID(testEvent.ID),
|
||||
WithPubKey(testEvent.PubKey),
|
||||
WithCreatedAt(testEvent.CreatedAt),
|
||||
WithKind(testEvent.Kind),
|
||||
WithContent(testEvent.Content),
|
||||
WithSig(""),
|
||||
),
|
||||
expectedError: "signature must be 128 hex characters",
|
||||
},
|
||||
|
||||
{
|
||||
name: "short signature",
|
||||
event: Event{
|
||||
ID: testEvent.ID,
|
||||
PubKey: testEvent.PubKey,
|
||||
CreatedAt: testEvent.CreatedAt,
|
||||
Kind: testEvent.Kind,
|
||||
Tags: testEvent.Tags,
|
||||
Content: testEvent.Content,
|
||||
Sig: "abc123",
|
||||
},
|
||||
event: NewEvent(
|
||||
WithID(testEvent.ID),
|
||||
WithPubKey(testEvent.PubKey),
|
||||
WithCreatedAt(testEvent.CreatedAt),
|
||||
WithKind(testEvent.Kind),
|
||||
WithContent(testEvent.Content),
|
||||
WithSig("abc123"),
|
||||
),
|
||||
expectedError: "signature must be 128 hex characters",
|
||||
},
|
||||
|
||||
{
|
||||
name: "empty tag",
|
||||
event: Event{
|
||||
ID: testEvent.ID,
|
||||
PubKey: testEvent.PubKey,
|
||||
CreatedAt: testEvent.CreatedAt,
|
||||
Kind: testEvent.Kind,
|
||||
Tags: []Tag{{}},
|
||||
Content: testEvent.Content,
|
||||
Sig: testEvent.Sig,
|
||||
},
|
||||
event: NewEvent(
|
||||
WithID(testEvent.ID),
|
||||
WithPubKey(testEvent.PubKey),
|
||||
WithCreatedAt(testEvent.CreatedAt),
|
||||
WithKind(testEvent.Kind),
|
||||
WithTag(Tag{}),
|
||||
WithContent(testEvent.Content),
|
||||
WithSig(testEvent.Sig),
|
||||
),
|
||||
expectedError: "tags must contain at least two elements",
|
||||
},
|
||||
|
||||
{
|
||||
name: "single element tag",
|
||||
event: Event{
|
||||
ID: testEvent.ID,
|
||||
PubKey: testEvent.PubKey,
|
||||
CreatedAt: testEvent.CreatedAt,
|
||||
Kind: testEvent.Kind,
|
||||
Tags: []Tag{{"a"}},
|
||||
Content: testEvent.Content,
|
||||
Sig: testEvent.Sig,
|
||||
},
|
||||
event: NewEvent(
|
||||
WithID(testEvent.ID),
|
||||
WithPubKey(testEvent.PubKey),
|
||||
WithCreatedAt(testEvent.CreatedAt),
|
||||
WithKind(testEvent.Kind),
|
||||
WithTag(Tag{"a"}),
|
||||
WithContent(testEvent.Content),
|
||||
WithSig(testEvent.Sig),
|
||||
),
|
||||
expectedError: "tags must contain at least two elements",
|
||||
},
|
||||
|
||||
{
|
||||
name: "one good tag, one single element tag",
|
||||
event: Event{
|
||||
ID: testEvent.ID,
|
||||
PubKey: testEvent.PubKey,
|
||||
CreatedAt: testEvent.CreatedAt,
|
||||
Kind: testEvent.Kind,
|
||||
Tags: []Tag{{"a", "value"}, {"b"}},
|
||||
Content: testEvent.Content,
|
||||
Sig: testEvent.Sig,
|
||||
},
|
||||
event: NewEvent(
|
||||
WithID(testEvent.ID),
|
||||
WithPubKey(testEvent.PubKey),
|
||||
WithCreatedAt(testEvent.CreatedAt),
|
||||
WithKind(testEvent.Kind),
|
||||
WithTag(Tag{"a", "value"}),
|
||||
WithTag(Tag{"b"}),
|
||||
WithContent(testEvent.Content),
|
||||
WithSig(testEvent.Sig),
|
||||
),
|
||||
expectedError: "tags must contain at least two elements",
|
||||
},
|
||||
}
|
||||
@@ -191,37 +183,36 @@ func TestValidateEventStructure(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestValidateEventIDFailure(t *testing.T) {
|
||||
event := Event{
|
||||
ID: "7f661c2a3c1ed67dc959d6cd968d743d5e6e334313df44724bca939e2aa42c9e",
|
||||
PubKey: testEvent.PubKey,
|
||||
CreatedAt: testEvent.CreatedAt,
|
||||
Kind: testEvent.Kind,
|
||||
Tags: testEvent.Tags,
|
||||
Content: testEvent.Content,
|
||||
Sig: testEvent.Sig,
|
||||
}
|
||||
event := NewEvent(
|
||||
WithID("7f661c2a3c1ed67dc959d6cd968d743d5e6e334313df44724bca939e2aa42c9e"),
|
||||
WithPubKey(testEvent.PubKey),
|
||||
WithCreatedAt(testEvent.CreatedAt),
|
||||
WithKind(testEvent.Kind),
|
||||
WithContent(testEvent.Content),
|
||||
WithSig(testEvent.Sig),
|
||||
)
|
||||
|
||||
err := ValidateID(event)
|
||||
assert.ErrorContains(t, err, "does not match computed id")
|
||||
}
|
||||
|
||||
func TestValidateSignature(t *testing.T) {
|
||||
event := Event{
|
||||
ID: testEvent.ID,
|
||||
PubKey: testEvent.PubKey,
|
||||
Sig: testEvent.Sig,
|
||||
}
|
||||
event := NewEvent(
|
||||
WithID(testEvent.ID),
|
||||
WithPubKey(testEvent.PubKey),
|
||||
WithSig(testEvent.Sig),
|
||||
)
|
||||
err := ValidateSignature(event)
|
||||
|
||||
assert.NoError(t, err)
|
||||
}
|
||||
|
||||
func TestValidateInvalidSignature(t *testing.T) {
|
||||
event := Event{
|
||||
ID: testEvent.ID,
|
||||
PubKey: testEvent.PubKey,
|
||||
Sig: "9e43cbcf7e828a21c53fa35371ee79bffbfd7a3063ae46fc05ec623dd3186667c57e3d006488015e19247df35eb41c61013e051aa87860e23fa5ffbd44120482",
|
||||
}
|
||||
event := NewEvent(
|
||||
WithID(testEvent.ID),
|
||||
WithPubKey(testEvent.PubKey),
|
||||
WithSig("9e43cbcf7e828a21c53fa35371ee79bffbfd7a3063ae46fc05ec623dd3186667c57e3d006488015e19247df35eb41c61013e051aa87860e23fa5ffbd44120482"),
|
||||
)
|
||||
err := ValidateSignature(event)
|
||||
|
||||
assert.ErrorContains(t, err, "event signature is invalid")
|
||||
@@ -280,7 +271,11 @@ var validateSignatureTestCases = []ValidateSignatureTestCase{
|
||||
func TestValidateSignatureInvalidEventSignature(t *testing.T) {
|
||||
for _, tc := range validateSignatureTestCases {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
event := Event{ID: tc.id, PubKey: tc.pubkey, Sig: tc.sig}
|
||||
event := NewEvent(
|
||||
WithID(tc.id),
|
||||
WithPubKey(tc.pubkey),
|
||||
WithSig(tc.sig),
|
||||
)
|
||||
err := ValidateSignature(event)
|
||||
assert.ErrorContains(t, err, tc.expectedError)
|
||||
})
|
||||
@@ -288,18 +283,16 @@ func TestValidateSignatureInvalidEventSignature(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestValidateEvent(t *testing.T) {
|
||||
event := Event{
|
||||
ID: "c9a0f84fcaa889654da8992105eb122eb210c8cbd58210609a5ef7e170b51400",
|
||||
PubKey: testEvent.PubKey,
|
||||
CreatedAt: testEvent.CreatedAt,
|
||||
Kind: testEvent.Kind,
|
||||
Tags: []Tag{
|
||||
{"a", "value"},
|
||||
{"b", "value", "optional"},
|
||||
},
|
||||
Content: "valid event",
|
||||
Sig: "668a715f1eb983172acf230d17bd283daedb2598adf8de4290bcc7eb0b802fdb60669d1e7d1104ac70393f4dbccd07e8abf897152af6ce6c0a75499874e27f14",
|
||||
}
|
||||
event := NewEvent(
|
||||
WithID("c9a0f84fcaa889654da8992105eb122eb210c8cbd58210609a5ef7e170b51400"),
|
||||
WithPubKey(testEvent.PubKey),
|
||||
WithCreatedAt(testEvent.CreatedAt),
|
||||
WithKind(testEvent.Kind),
|
||||
WithTag(Tag{"a", "value"}),
|
||||
WithTag(Tag{"b", "value", "optional"}),
|
||||
WithContent("valid event"),
|
||||
WithSig("668a715f1eb983172acf230d17bd283daedb2598adf8de4290bcc7eb0b802fdb60669d1e7d1104ac70393f4dbccd07e8abf897152af6ce6c0a75499874e27f14"),
|
||||
)
|
||||
|
||||
err := Validate(event)
|
||||
assert.NoError(t, err)
|
||||
|
||||
Reference in New Issue
Block a user