Skip to content

Commit 83b94d0

Browse files
authored
fix: prevent creating a mod reference matching existing id (#117)
* fix: prevent creating a mod reference matching existing id * chore: reword comment
1 parent 85dd520 commit 83b94d0

File tree

2 files changed

+59
-0
lines changed

2 files changed

+59
-0
lines changed

gql/resolver_mods.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,16 @@ func (r *mutationResolver) CreateMod(ctx context.Context, newMod generated.NewMo
6161
return nil, errors.New("mod with this mod reference already exists")
6262
}
6363

64+
// Check if mod reference matches any existing mod ID
65+
existByID, err := db.From(ctx).Mod.Query().Where(mod.ID(newMod.ModReference)).Exist(ctx)
66+
if err != nil {
67+
return nil, err
68+
}
69+
70+
if existByID {
71+
return nil, errors.New("mod reference cannot match an existing mod ID")
72+
}
73+
6474
dbMod := db.From(ctx).Mod.Create().
6575
SetName(newMod.Name).
6676
SetShortDescription(newMod.ShortDescription).

tests/mod_test.go

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -341,4 +341,53 @@ func TestMods(t *testing.T) {
341341
}
342342
})
343343
}
344+
345+
t.Run("Prevent Mod Reference Matching Existing Mod ID", func(t *testing.T) {
346+
// First create a mod to get its ID
347+
token, _, err := makeUser(ctx)
348+
testza.AssertNoError(t, err)
349+
350+
// Create the first mod
351+
createRequest := authRequest(`mutation ($mod_reference: ModReference!) {
352+
createMod(mod: {
353+
name: "First Mod",
354+
short_description: "This is the first mod for testing",
355+
full_description: "A full description for the first mod",
356+
mod_reference: $mod_reference
357+
}) {
358+
id
359+
}
360+
}`, token)
361+
createRequest.Var("mod_reference", "first-mod")
362+
363+
var createResponse struct {
364+
CreateMod generated.Mod
365+
}
366+
testza.AssertNoError(t, client.Run(ctx, createRequest, &createResponse))
367+
testza.AssertNotEqual(t, "", createResponse.CreateMod.ID)
368+
369+
existingModID := createResponse.CreateMod.ID
370+
371+
// Now try to create another mod with a reference that matches the first mod's ID
372+
// This should fail
373+
createRequest2 := authRequest(`mutation ($mod_reference: ModReference!) {
374+
createMod(mod: {
375+
name: "Second Mod",
376+
short_description: "This should fail because reference matches existing mod ID",
377+
full_description: "A full description for the second mod",
378+
mod_reference: $mod_reference
379+
}) {
380+
id
381+
}
382+
}`, token)
383+
createRequest2.Var("mod_reference", existingModID) // Using existing mod ID as reference
384+
385+
var createResponse2 struct {
386+
CreateMod generated.Mod
387+
}
388+
389+
err = client.Run(ctx, createRequest2, &createResponse2)
390+
testza.AssertNotNil(t, err)
391+
testza.AssertContains(t, err.Error(), "mod reference cannot match an existing mod ID")
392+
})
344393
}

0 commit comments

Comments
 (0)