Skip to content

Commit

Permalink
feat: GET /challenge?challenge_id=xxxd
Browse files Browse the repository at this point in the history
  • Loading branch information
moul committed Oct 20, 2019
1 parent fb6976e commit d86334f
Show file tree
Hide file tree
Showing 9 changed files with 714 additions and 96 deletions.
10 changes: 9 additions & 1 deletion api/pwengine.proto
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ service Engine {
rpc GetUserSession(Void) returns (UserSessionOutput) { option (google.api.http) = {get: "/user-session"}; };
rpc SetPreferences(SetPreferencesInput) returns (Void) { option (google.api.http) = {post: "/preferences"; body: "*"}; };
rpc ListChallenges(Void) returns (ListChallengesOutput) { option (google.api.http) = {get: "/challenges"}; };
rpc GetChallenge(GetChallengeInput) returns (GetChallengeOutput) { option (google.api.http) = {get: "/challenge"}; };
rpc ListTeams(Void) returns (ListTeamsOutput) { option (google.api.http) = {get: "/teams"}; };
rpc ListTournamentTeams(ListTournamentTeamsInput) returns (ListTournamentTeamsOutput) { option (google.api.http) = {get: "/tournament/teams"}; };
rpc GetTournamentTeam(GetTournamentTeamInput) returns (GetTournamentTeamOutput) { option (google.api.http) = {get: "/tournament/team"}; };
Expand All @@ -113,7 +114,6 @@ message Void {
};
}


message GetTournamentTeamInput {
int64 tournament_team_id = 1 [(gogoproto.customname) = "TournamentTeamID"];
}
Expand All @@ -122,6 +122,14 @@ message GetTournamentTeamOutput {
pathwar.db.TournamentTeam item = 1;
}

message GetChallengeInput {
int64 challenge_id = 1 [(gogoproto.customname) = "ChallengeID"];
}

message GetChallengeOutput {
pathwar.db.Challenge item = 1;
}

message ListTournamentTeamsInput {
int64 tournament_id = 1 [(gogoproto.customname) = "TournamentID"];
}
Expand Down
2 changes: 1 addition & 1 deletion docs/gen.sum

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

2 changes: 1 addition & 1 deletion go/gen.sum

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

36 changes: 36 additions & 0 deletions go/pkg/pwengine/api_pub_getchallenge.go
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
}
74 changes: 74 additions & 0 deletions go/pkg/pwengine/api_pub_getchallenge_test.go
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)
}
})
}
}
Loading

0 comments on commit d86334f

Please sign in to comment.