forked from pathwar/pathwar
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: GET /challenge?challenge_id=xxxd
- Loading branch information
Showing
9 changed files
with
714 additions
and
96 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
package pwengine | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
|
||
"pathwar.land/go/pkg/pwdb" | ||
) | ||
|
||
func (e *engine) GetChallenge(ctx context.Context, in *GetChallengeInput) (*GetChallengeOutput, error) { | ||
{ // validation | ||
if in.ChallengeID == 0 { | ||
return nil, ErrMissingArgument | ||
} | ||
} | ||
|
||
var item pwdb.Challenge | ||
err := e.db. | ||
Set("gorm:auto_preload", true). | ||
Where(pwdb.Challenge{ID: in.ChallengeID}). | ||
First(&item). | ||
Error | ||
|
||
switch { | ||
case err != nil && pwdb.IsRecordNotFoundError(err): | ||
return nil, ErrInvalidArgument // FIXME: wrap original error | ||
case err != nil: | ||
return nil, fmt.Errorf("query challenge: %w", err) | ||
} | ||
|
||
ret := GetChallengeOutput{ | ||
Item: &item, | ||
} | ||
|
||
return &ret, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
package pwengine | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
"testing" | ||
|
||
"pathwar.land/go/internal/testutil" | ||
) | ||
|
||
func TestEngine_GetChallenge(t *testing.T) { | ||
engine, cleanup := TestingEngine(t, Opts{Logger: testutil.Logger(t)}) | ||
defer cleanup() | ||
ctx := testingSetContextToken(context.Background(), t) | ||
|
||
// FIXME: check for permissions | ||
|
||
challenges := map[string]int64{} | ||
for _, challenge := range testingChallenges(t, engine).Items { | ||
challenges[challenge.Name] = challenge.ID | ||
} | ||
|
||
var tests = []struct { | ||
name string | ||
input *GetChallengeInput | ||
expectedErr error | ||
expectedChallengeName string | ||
expectedAuthor string | ||
}{ | ||
{ | ||
"empty", | ||
&GetChallengeInput{}, | ||
ErrMissingArgument, | ||
"", | ||
"", | ||
}, { | ||
"unknown-tournament-id", | ||
&GetChallengeInput{ChallengeID: -42}, // -42 should not exists | ||
ErrInvalidArgument, | ||
"", | ||
"", | ||
}, { | ||
"Staff", | ||
&GetChallengeInput{ChallengeID: challenges["Hello World (test)"]}, | ||
nil, | ||
"Hello World (test)", | ||
"m1ch3l", | ||
}, | ||
} | ||
|
||
for _, test := range tests { | ||
t.Run(test.name, func(t *testing.T) { | ||
ret, err := engine.GetChallenge(ctx, test.input) | ||
if !errors.Is(err, test.expectedErr) { | ||
t.Fatalf("Expected %#v, got %#v.", test.expectedErr, err) | ||
} | ||
if err != nil { | ||
return | ||
} | ||
|
||
// FIXME: check for ChallengeVersions and ChallengeInstances | ||
|
||
if ret.Item.ID != test.input.ChallengeID { | ||
t.Fatalf("Expected %q, got %q.", test.input.ChallengeID, ret.Item.ID) | ||
} | ||
if ret.Item.Name != test.expectedChallengeName { | ||
t.Fatalf("Expected %q, got %q.", test.expectedChallengeName, ret.Item.Name) | ||
} | ||
if ret.Item.Author != test.expectedAuthor { | ||
t.Fatalf("Expected %q, got %q.", test.expectedAuthor, ret.Item.Author) | ||
} | ||
}) | ||
} | ||
} |
Oops, something went wrong.