Skip to content

Commit

Permalink
test(notifier): add test for BasicGenerator (#34)
Browse files Browse the repository at this point in the history
  • Loading branch information
ripls56 authored Sep 20, 2024
2 parents c5757eb + d39f332 commit 340ff8e
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 5 deletions.
1 change: 0 additions & 1 deletion apps/server/internal/pkg/notifier/account.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ func NewEmailAccountNotifier(opts OptsEmailAccNotifier) *EmailAccountNotifier {
}

func (n *EmailAccountNotifier) VerifyEmail(username string, email string) error {

confirmLink, _, err := n.lg.VerifyLink()
if err != nil {
return errors.Wrap(err, "verify email")
Expand Down
4 changes: 2 additions & 2 deletions apps/server/internal/pkg/notifier/account_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,11 @@ type testGenerator struct {
}

func (t *testGenerator) VerifyLink() (link string, id uuid.UUID, err error) {
return fmt.Sprintf("%s/verify/id=%s", t.cfg.HostDomain, uuid.Nil), uuid.Nil, nil
return fmt.Sprintf("%s/verify?id=%s", t.cfg.HostDomain, uuid.Nil), uuid.Nil, nil
}

func (t *testGenerator) UnsubLink() (link string, id uuid.UUID, err error) {
return fmt.Sprintf("%s/unsub/id=%s", t.cfg.HostDomain, uuid.Nil), uuid.Nil, nil
return fmt.Sprintf("%s/unsub?id=%s", t.cfg.HostDomain, uuid.Nil), uuid.Nil, nil
}

type args struct {
Expand Down
4 changes: 2 additions & 2 deletions apps/server/internal/pkg/notifier/generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ func (b *BasicGenerator) VerifyLink() (string, uuid.UUID, error) {
_, err = sb.WriteString("/verify?id=")
err = multierr.Append(err, err)

_, err = sb.WriteString(b.HostDomain)
_, err = sb.WriteString(confirmID.String())
err = multierr.Append(err, err)

return sb.String(), confirmID, err
Expand All @@ -51,7 +51,7 @@ func (b *BasicGenerator) UnsubLink() (string, uuid.UUID, error) {
_, err = sb.WriteString("/unsub?id=")
err = multierr.Append(err, err)

_, err = sb.WriteString(b.HostDomain)
_, err = sb.WriteString(unsubID.String())
err = multierr.Append(err, err)

return sb.String(), unsubID, err
Expand Down
46 changes: 46 additions & 0 deletions apps/server/internal/pkg/notifier/generator_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package notifier

import (
"fmt"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"regexp"
"strconv"
"testing"
)

func TestBasicGenerator_VerifyLink(t *testing.T) {
regex, err := regexp.Compile(".*/verify\\?id=[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}")
require.NoError(t, err)
for i := range 10 {
t.Run(strconv.Itoa(i), func(t *testing.T) {
b := &BasicGenerator{
HostDomain: fmt.Sprintf("%d.com", i),
}
link, _, err := b.VerifyLink()
assert.NoError(t, err)

t.Log(link)
match := regex.Match([]byte(link))
assert.True(t, match)
})
}
}

func TestBasicGenerator_UnsubLink(t *testing.T) {
regex, err := regexp.Compile(".*/unsub\\?id=[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}")
require.NoError(t, err)
for i := range 10 {
t.Run(strconv.Itoa(i), func(t *testing.T) {
b := &BasicGenerator{
HostDomain: fmt.Sprintf("%d.com", i),
}
link, _, err := b.UnsubLink()
assert.NoError(t, err)

t.Log(link)
match := regex.Match([]byte(link))
assert.True(t, match)
})
}
}

0 comments on commit 340ff8e

Please sign in to comment.