Skip to content

Commit

Permalink
fix: increase token size limit
Browse files Browse the repository at this point in the history
fix: guide tag collisions
fix: tag bulk create
test: add tests for tags
  • Loading branch information
Vilsol committed Jun 29, 2024
1 parent 4a30196 commit 2236623
Show file tree
Hide file tree
Showing 12 changed files with 443 additions and 29 deletions.
2 changes: 1 addition & 1 deletion db/schema/user_session.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ func (UserSession) Mixin() []ent.Mixin {

func (UserSession) Fields() []ent.Field {
return []ent.Field{
field.String("token").MaxLen(256).Unique(),
field.String("token").MaxLen(512).Unique(),
field.String("user_agent").Optional(),
}
}
Expand Down
2 changes: 1 addition & 1 deletion generated/ent/internal/schema.go

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion generated/ent/migrate/schema.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion gql/resolver_guides.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ func (r *mutationResolver) UpdateGuide(ctx context.Context, guideID string, g ge

return tx.GuideTag.MapCreateBulk(g.TagIDs, func(create *ent.GuideTagCreate, i int) {
create.SetGuideID(guideID).SetTagID(g.TagIDs[i])
}).Exec(ctx)
}).OnConflict(sql.DoNothing()).Exec(ctx)
}, nil); err != nil {
return nil, err
}
Expand Down
15 changes: 11 additions & 4 deletions gql/resolver_tags.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,17 @@ func (r *mutationResolver) CreateMultipleTags(ctx context.Context, tags []*gener
}
}

result, err := db.From(ctx).Tag.MapCreateBulk(tags, func(create *ent.TagCreate, i int) {
create.SetName(tags[i].Name).SetDescription(tags[i].Description)
}).Save(ctx)
if err != nil {
var result []*ent.Tag
if err := db.Tx(ctx, func(ctx context.Context, tx *ent.Tx) error {
for _, newTag := range tags {
t, err := tx.Tag.Create().SetName(newTag.Name).SetDescription(newTag.Description).Save(ctx)
if err != nil {
return err
}
result = append(result, t)
}
return nil
}, nil); err != nil {
return nil, err
}

Expand Down
2 changes: 2 additions & 0 deletions migrations/sql/000028_increase_token_length_limit.down.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
ALTER TABLE user_sessions
ALTER COLUMN token TYPE varchar(256);
2 changes: 2 additions & 0 deletions migrations/sql/000028_increase_token_length_limit.up.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
ALTER TABLE user_sessions
ALTER COLUMN token TYPE varchar(512);
48 changes: 43 additions & 5 deletions tests/guides_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ func init() {
db.EnableDebug()
}

// TODO Add guide tag test
// TODO Add rate limit test

func TestGuides(t *testing.T) {
Expand All @@ -28,21 +27,25 @@ func TestGuides(t *testing.T) {
token, userID, err := makeUser(ctx)
testza.AssertNoError(t, err)

tags := seedTags(ctx, t, token, client)

// Run Twice to detect any cache issues
for i := range 2 {
t.Run("Loop"+strconv.Itoa(i), func(t *testing.T) {
var guideID string

t.Run("Create", func(t *testing.T) {
createGuide := authRequest(`mutation {
createGuide := authRequest(`mutation ($tags: [TagID!]) {
createGuide(guide: {
name: "Hello World",
short_description: "Short description about the guide",
guide: "The full guide text goes here."
guide: "The full guide text goes here.",
tagIDs: $tags
}) {
id
}
}`, token)
createGuide.Var("tags", tags)

var createGuideResponse struct {
CreateGuide generated.Guide
Expand Down Expand Up @@ -79,17 +82,19 @@ func TestGuides(t *testing.T) {
})

t.Run("Update", func(t *testing.T) {
updateGuide := authRequest(`mutation ($id: GuideID!) {
updateGuide := authRequest(`mutation ($id: GuideID!, $tags: [TagID!]) {
updateGuide(
guideId: $id,
guide: {
name: "Foo Bar"
name: "Foo Bar",
tagIDs: $tags
}
) {
id
}
}`, token)
updateGuide.Var("id", guideID)
updateGuide.Var("tags", tags)

var updateGuideResponse struct {
UpdateGuide generated.Guide
Expand Down Expand Up @@ -126,6 +131,39 @@ func TestGuides(t *testing.T) {
testza.AssertEqual(t, userID, queryGuidesResponse.GetGuides.Guides[0].User.ID)
})

t.Run("Search", func(t *testing.T) {
searchGuides := authRequest(`query ($tags: [TagID!]) {
getGuides (filter: {
search: "Bar",
tagIDs: $tags
}){
count
guides {
id
name
short_description
guide
user {
id
}
}
}
}`, token)
searchGuides.Var("tags", tags[:1])

var queryGuidesResponse struct {
GetGuides generated.GetGuides
}
testza.AssertNoError(t, client.Run(ctx, searchGuides, &queryGuidesResponse))
testza.AssertEqual(t, 1, queryGuidesResponse.GetGuides.Count)
testza.AssertEqual(t, 1, len(queryGuidesResponse.GetGuides.Guides))
testza.AssertEqual(t, guideID, queryGuidesResponse.GetGuides.Guides[0].ID)
testza.AssertEqual(t, "Foo Bar", queryGuidesResponse.GetGuides.Guides[0].Name)
testza.AssertEqual(t, "Short description about the guide", queryGuidesResponse.GetGuides.Guides[0].ShortDescription)
testza.AssertEqual(t, "The full guide text goes here.", queryGuidesResponse.GetGuides.Guides[0].Guide)
testza.AssertEqual(t, userID, queryGuidesResponse.GetGuides.Guides[0].User.ID)
})

t.Run("Delete", func(t *testing.T) {
deleteGuide := authRequest(`mutation ($id: GuideID!) {
deleteGuide(guideId: $id)
Expand Down
Loading

0 comments on commit 2236623

Please sign in to comment.