henwen/internal/back/store_test.go

303 lines
8.2 KiB
Go
Raw Permalink Normal View History

2020-04-16 20:49:28 +00:00
package back_test
import (
"context"
2020-10-04 20:54:41 +00:00
"math/rand"
"sort"
2020-04-16 20:49:28 +00:00
"testing"
2020-10-04 20:54:41 +00:00
"time"
2020-04-16 20:49:28 +00:00
"github.com/matryer/is"
2020-04-21 14:25:15 +00:00
"github.com/rickb777/date"
2020-10-11 18:21:17 +00:00
"gitlab.codemonkeysoftware.net/b/henwen/internal/back"
2020-04-16 20:49:28 +00:00
)
func TestNewMemoryStore(t *testing.T) {
is := is.New(t)
store, err := back.NewMemoryStore(back.SecureGenString)
is.NoErr(err)
is.True(store != nil)
store.Close()
}
func TestCreateEvent(t *testing.T) {
is := is.New(t)
store, err := back.NewMemoryStore(back.SecureGenString)
is.NoErr(err)
defer store.Close()
2020-04-21 14:25:15 +00:00
earliest := date.New(2020, 4, 5)
latest := date.New(2020, 4, 5)
2020-04-16 20:49:28 +00:00
const name = "abc"
const description = "def"
createResult, err := store.CreateEvent(context.Background(), back.CreateEventCommand{
2020-04-21 14:25:15 +00:00
Name: name,
Description: description,
Earliest: earliest,
Latest: latest,
2020-04-16 20:49:28 +00:00
})
is.NoErr(err)
metadataResult, err := store.GetEventMetadata(context.Background(), back.GetEventMetadataQuery{
2020-09-29 15:25:21 +00:00
EventID: createResult.EventID,
2020-04-16 20:49:28 +00:00
})
is.NoErr(err)
is.Equal(metadataResult.Name, name)
is.Equal(metadataResult.Description, description)
2020-04-21 14:25:15 +00:00
is.True(metadataResult.Earliest.Equal(earliest))
is.True(metadataResult.Latest.Equal(latest))
2020-04-16 20:49:28 +00:00
}
2020-04-17 02:36:48 +00:00
2020-09-27 21:44:04 +00:00
func TestGetEventResponseSummary(t *testing.T) {
2020-04-17 02:36:48 +00:00
store, err := back.NewMemoryStore(back.SecureGenString)
is.New(t).NoErr(err)
2020-09-27 21:44:04 +00:00
defer store.Close()
2020-04-17 02:36:48 +00:00
createEvent := func(is *is.I) (eventID string) {
event, err := store.CreateEvent(context.Background(), back.CreateEventCommand{
2020-04-21 14:25:15 +00:00
Name: "blah",
2020-04-17 02:36:48 +00:00
Description: "stuff happening",
2020-04-21 14:25:15 +00:00
Earliest: date.Today(),
Latest: date.Today().Add(1),
2020-04-17 02:36:48 +00:00
})
is.NoErr(err)
2020-09-29 15:25:21 +00:00
return event.EventID
2020-04-17 02:36:48 +00:00
}
2020-10-04 20:24:04 +00:00
t.Run("does not include responses to other events", func(t *testing.T) {
2020-04-17 02:36:48 +00:00
is := is.New(t)
2020-10-04 20:24:04 +00:00
d := date.New(1999, 12, 31)
wrongEvent, err := store.CreateEvent(context.Background(), back.CreateEventCommand{
Name: "Beach party",
Description: "Come on in! The water's fine!",
Earliest: d,
Latest: d,
})
is.NoErr(err)
2020-04-17 02:36:48 +00:00
2020-10-04 20:24:04 +00:00
_, err = store.CreateEventResponse(context.Background(), back.CreateEventResponseCommand{
EventID: wrongEvent.EventID,
GuestName: "Matt Hooper",
DateHours: map[date.Date]map[int]struct{}{d: {0: {}}},
})
is.NoErr(err)
summary, err := store.GetEventResponseSummary(context.Background(), back.GetEventResponseSummaryQuery{
EventID: createEvent(is),
})
is.NoErr(err)
2020-04-17 02:36:48 +00:00
2020-10-04 20:24:04 +00:00
is.Equal(summary, back.GetEventResponseSummaryResult{
2020-10-04 20:54:41 +00:00
RespondentNames: nil,
PerHourCounts: map[date.Date]map[int]int{},
})
})
t.Run("RespondentNames lists all respondents alphabetically", func(t *testing.T) {
is := is.New(t)
var names = []string{"a", "b", "c", "d", "e", "f", "g", "h", "i", "j"}
rand.Shuffle(len(names), func(i, j int) {
names[i], names[j] = names[j], names[i]
2020-10-04 20:24:04 +00:00
})
2020-10-04 20:54:41 +00:00
d := date.New(1963, time.May, 1)
event, err := store.CreateEvent(context.Background(), back.CreateEventCommand{
Name: "It's my party!",
Description: "I can cry if I want to.",
Latest: d,
Earliest: d,
})
is.NoErr(err)
for _, name := range names {
_, err = store.CreateEventResponse(context.Background(), back.CreateEventResponseCommand{
EventID: event.EventID,
GuestName: name,
})
is.NoErr(err)
}
summary, err := store.GetEventResponseSummary(context.Background(), back.GetEventResponseSummaryQuery{
EventID: event.EventID,
})
is.NoErr(err)
sort.Strings(names)
is.Equal(summary.RespondentNames, names)
2020-04-17 02:36:48 +00:00
})
2020-09-27 21:44:04 +00:00
2020-10-04 20:24:04 +00:00
t.Run("PerHourCounts counts votes per hour", func(t *testing.T) {
is := is.New(t)
dayOne := date.New(2012, 12, 21)
dayTwo := dayOne.Add(1)
createResult, err := store.CreateEvent(context.Background(), back.CreateEventCommand{
Name: "Apocalypse",
Description: "It's over.",
Earliest: dayOne,
Latest: dayTwo,
})
is.NoErr(err)
_, err = store.CreateEventResponse(context.Background(), back.CreateEventResponseCommand{
EventID: createResult.EventID,
GuestName: "Thanos",
DateHours: map[date.Date]map[int]struct{}{
dayOne: {0: {}, 1: {}, 2: {}, 3: {}},
dayTwo: {1: {}, 2: {}, 3: {}, 4: {}},
},
})
is.NoErr(err)
_, err = store.CreateEventResponse(context.Background(), back.CreateEventResponseCommand{
EventID: createResult.EventID,
GuestName: "Magog",
DateHours: map[date.Date]map[int]struct{}{
dayOne: {1: {}, 2: {}, 3: {}, 4: {}},
dayTwo: {2: {}, 3: {}, 4: {}, 5: {}},
},
})
is.NoErr(err)
summary, err := store.GetEventResponseSummary(context.Background(), back.GetEventResponseSummaryQuery{
EventID: createResult.EventID,
})
is.NoErr(err)
is.Equal(summary.PerHourCounts, map[date.Date]map[int]int{
dayOne: {0: 1, 1: 2, 2: 2, 3: 2, 4: 1},
dayTwo: {1: 1, 2: 2, 3: 2, 4: 2, 5: 1},
})
})
2020-09-27 21:44:04 +00:00
}
func TestCreateEventResponse(t *testing.T) {
store, err := back.NewMemoryStore(back.SecureGenString)
is.New(t).NoErr(err)
2020-10-04 22:03:16 +00:00
defer store.Close()
2020-09-27 21:44:04 +00:00
createEvent := func(is *is.I) (eventID string) {
event, err := store.CreateEvent(context.Background(), back.CreateEventCommand{
Name: "blah",
Description: "stuff happening",
Earliest: date.Today(),
Latest: date.Today().Add(1),
})
is.NoErr(err)
2020-09-29 15:25:21 +00:00
return event.EventID
2020-09-27 21:44:04 +00:00
}
t.Run("saves GuestName", func(t *testing.T) {
is := is.New(t)
eventID := createEvent(is)
const guestName = "Etaoin Shrdlu"
createResult, err := store.CreateEventResponse(context.Background(), back.CreateEventResponseCommand{
2020-09-29 15:25:21 +00:00
EventID: eventID,
GuestName: guestName,
2020-09-27 21:44:04 +00:00
})
is.NoErr(err)
getResult, err := store.GetEventResponse(context.Background(), back.GetEventResponseQuery{
2020-09-29 15:25:21 +00:00
EventID: eventID,
ResponseID: createResult.ResponseID,
2020-09-27 21:44:04 +00:00
})
is.NoErr(err)
is.Equal(getResult.GuestName, guestName)
})
t.Run("DateHours saves multiple times on multiple dates", func(t *testing.T) {
is := is.New(t)
earliest := date.Today()
latest := earliest.Add(1)
event, err := store.CreateEvent(context.Background(), back.CreateEventCommand{
Name: "blah",
Description: "stuff happening",
Earliest: earliest,
Latest: latest,
})
is.NoErr(err)
var dateHours = map[date.Date]map[int]struct{}{
earliest: {1: {}, 3: {}, 5: {}, 7: {}},
latest: {2: {}, 4: {}, 6: {}},
}
createResponseResult, err := store.CreateEventResponse(context.Background(), back.CreateEventResponseCommand{
2020-09-29 15:25:21 +00:00
EventID: event.EventID,
GuestName: "Etaoin Shrdlu",
DateHours: dateHours,
2020-09-27 21:44:04 +00:00
})
is.NoErr(err)
response, err := store.GetEventResponse(context.Background(), back.GetEventResponseQuery{
2020-09-29 15:25:21 +00:00
EventID: event.EventID,
ResponseID: createResponseResult.ResponseID,
2020-09-27 21:44:04 +00:00
})
is.NoErr(err)
is.Equal(dateHours, response.DateHours)
})
}
2020-10-04 22:03:16 +00:00
func TestAuthorizeEventAdmin(t *testing.T) {
store, err := back.NewMemoryStore(back.SecureGenString)
is.New(t).NoErr(err)
defer store.Close()
t.Run("returns ErrUnauthorized if admin code is wrong", func(t *testing.T) {
is := is.New(t)
event, err := store.CreateEvent(context.Background(), back.CreateEventCommand{
Name: "my event",
Description: "stuff happening",
Earliest: date.Today(),
Latest: date.Today(),
})
is.NoErr(err)
badAdminCode := event.AdminCode + "x"
err = store.AuthorizeEventAdmin(context.Background(), back.CheckEventAdminCodeQuery{
EventID: event.EventID,
AdminCode: badAdminCode,
})
is.Equal(err, back.UnauthorizedError{
EventID: event.EventID,
AdminCode: badAdminCode,
})
})
t.Run("return ErrUnauthorized if event does not exist", func(t *testing.T) {
is := is.New(t)
randString, err := back.SecureGenString(10)
is.NoErr(err)
err = store.AuthorizeEventAdmin(context.Background(), back.CheckEventAdminCodeQuery{
EventID: randString,
AdminCode: randString,
})
is.Equal(err, back.UnauthorizedError{
EventID: randString,
AdminCode: randString,
})
})
t.Run("returns nil if admin code is correct", func(t *testing.T) {
is := is.New(t)
event, err := store.CreateEvent(context.Background(), back.CreateEventCommand{
Name: "my event",
Description: "stuff happening",
Earliest: date.Today(),
Latest: date.Today(),
})
is.NoErr(err)
err = store.AuthorizeEventAdmin(context.Background(), back.CheckEventAdminCodeQuery{
EventID: event.EventID,
AdminCode: event.AdminCode,
})
is.NoErr(err)
})
}