-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1161 from ictsc/admin-invitation-code
feat: implement admin.v1.InvitationService
- Loading branch information
Showing
16 changed files
with
861 additions
and
10 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,6 +13,7 @@ linters: | |
- ireturn | ||
- gofumpt | ||
- tagliatelle | ||
- funlen | ||
|
||
issues: | ||
exclude-use-default: false | ||
|
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
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
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
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,89 @@ | ||
package admin | ||
|
||
import ( | ||
"context" | ||
|
||
"connectrpc.com/connect" | ||
adminv1 "github.com/ictsc/ictsc-regalia/backend/pkg/proto/admin/v1" | ||
"github.com/ictsc/ictsc-regalia/backend/pkg/proto/admin/v1/adminv1connect" | ||
"github.com/ictsc/ictsc-regalia/backend/scoreserver/admin/auth" | ||
"github.com/ictsc/ictsc-regalia/backend/scoreserver/domain" | ||
"github.com/ictsc/ictsc-regalia/backend/scoreserver/infra/pg" | ||
"google.golang.org/protobuf/types/known/timestamppb" | ||
) | ||
|
||
type InvitationServiceHandler struct { | ||
adminv1connect.UnimplementedInvitationServiceHandler | ||
Enforcer *auth.Enforcer | ||
ListWorkflow *domain.InvitationCodeListWorkflow | ||
CreateWorkflow *domain.InvitationCodeCreateWorkflow | ||
} | ||
|
||
func NewInvitationServiceHandler(enforcer *auth.Enforcer, repo *pg.Repository) *InvitationServiceHandler { | ||
return &InvitationServiceHandler{ | ||
UnimplementedInvitationServiceHandler: adminv1connect.UnimplementedInvitationServiceHandler{}, | ||
|
||
Enforcer: enforcer, | ||
ListWorkflow: &domain.InvitationCodeListWorkflow{Lister: repo}, | ||
CreateWorkflow: &domain.InvitationCodeCreateWorkflow{ | ||
TeamGetter: repo, | ||
RunTx: func(ctx context.Context, f func(eff domain.InvitationCodeCreator) error) error { | ||
return repo.RunTx(ctx, func(tx *pg.RepositoryTx) error { return f(tx) }) | ||
}, | ||
}, | ||
} | ||
} | ||
|
||
var _ adminv1connect.InvitationServiceHandler = (*InvitationServiceHandler)(nil) | ||
|
||
func (h *InvitationServiceHandler) ListInvitationCodes( | ||
ctx context.Context, | ||
req *connect.Request[adminv1.ListInvitationCodesRequest], | ||
) (*connect.Response[adminv1.ListInvitationCodesResponse], error) { | ||
if err := enforce(ctx, h.Enforcer, "invitation_codes", "list"); err != nil { | ||
return nil, err | ||
} | ||
|
||
ics, err := h.ListWorkflow.Run(ctx) | ||
if err != nil { | ||
return nil, connectError(err) | ||
} | ||
|
||
protoICs := make([]*adminv1.InvitationCode, 0, len(ics)) | ||
for _, ic := range ics { | ||
protoICs = append(protoICs, convertInvitationCode(ic)) | ||
} | ||
|
||
return connect.NewResponse(&adminv1.ListInvitationCodesResponse{ | ||
InvitationCodes: protoICs, | ||
}), nil | ||
} | ||
|
||
func (h *InvitationServiceHandler) CreateInvitationCode( | ||
ctx context.Context, | ||
req *connect.Request[adminv1.CreateInvitationCodeRequest], | ||
) (*connect.Response[adminv1.CreateInvitationCodeResponse], error) { | ||
if err := enforce(ctx, h.Enforcer, "invitation_codes", "create"); err != nil { | ||
return nil, err | ||
} | ||
|
||
code, err := h.CreateWorkflow.Run(ctx, domain.InvitationCodeCreateInput{ | ||
TeamCode: int(req.Msg.GetInvitationCode().GetTeamCode()), | ||
ExpiresAt: req.Msg.GetInvitationCode().GetExpiresAt().AsTime(), | ||
}) | ||
if err != nil { | ||
return nil, connectError(err) | ||
} | ||
|
||
return connect.NewResponse(&adminv1.CreateInvitationCodeResponse{ | ||
InvitationCode: convertInvitationCode(code), | ||
}), nil | ||
} | ||
|
||
func convertInvitationCode(ic *domain.InvitationCode) *adminv1.InvitationCode { | ||
return &adminv1.InvitationCode{ | ||
Code: ic.Code(), | ||
TeamCode: int64(ic.Team().Code()), | ||
ExpiresAt: timestamppb.New(ic.ExpiresAt()), | ||
} | ||
} |
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,86 @@ | ||
package admin_test | ||
|
||
import ( | ||
"context" | ||
"net/http" | ||
"testing" | ||
"time" | ||
|
||
"connectrpc.com/connect" | ||
"github.com/google/go-cmp/cmp" | ||
"github.com/google/go-cmp/cmp/cmpopts" | ||
"github.com/ictsc/ictsc-regalia/backend/pkg/pgtest" | ||
adminv1 "github.com/ictsc/ictsc-regalia/backend/pkg/proto/admin/v1" | ||
"github.com/ictsc/ictsc-regalia/backend/pkg/proto/admin/v1/adminv1connect" | ||
"github.com/ictsc/ictsc-regalia/backend/scoreserver/admin" | ||
"github.com/ictsc/ictsc-regalia/backend/scoreserver/infra/pg" | ||
"google.golang.org/protobuf/types/known/timestamppb" | ||
) | ||
|
||
func TestAdminInvitationService_Create(t *testing.T) { | ||
t.Parallel() | ||
|
||
now := time.Now() | ||
|
||
cases := map[string]struct { | ||
in *adminv1.CreateInvitationCodeRequest | ||
wants *adminv1.CreateInvitationCodeResponse | ||
wantCode connect.Code | ||
}{ | ||
"ok": { | ||
in: &adminv1.CreateInvitationCodeRequest{ | ||
InvitationCode: &adminv1.InvitationCode{ | ||
TeamCode: 1, | ||
ExpiresAt: timestamppb.New(now.Add(24 * time.Hour)), | ||
}, | ||
}, | ||
wants: &adminv1.CreateInvitationCodeResponse{ | ||
InvitationCode: &adminv1.InvitationCode{ | ||
TeamCode: 1, | ||
ExpiresAt: timestamppb.New(now.Add(24 * time.Hour)), | ||
}, | ||
}, | ||
}, | ||
} | ||
|
||
for name, tt := range cases { | ||
t.Run(name, func(t *testing.T) { | ||
t.Parallel() | ||
|
||
ctx := context.Background() | ||
|
||
enforcer := setupEnforcer(t) | ||
|
||
db, ok := pgtest.SetupDB(t) | ||
if !ok { | ||
t.FailNow() | ||
} | ||
teamFixtures(db) | ||
|
||
mux := http.NewServeMux() | ||
mux.Handle(adminv1connect.NewInvitationServiceHandler(admin.NewInvitationServiceHandler( | ||
enforcer, pg.NewRepository(db), | ||
))) | ||
|
||
server := setupServer(t, mux) | ||
client := adminv1connect.NewInvitationServiceClient(server.Client(), server.URL) | ||
|
||
resp, err := client.CreateInvitationCode(ctx, connect.NewRequest(tt.in)) | ||
assertCode(t, tt.wantCode, err) | ||
if err != nil { | ||
return | ||
} | ||
|
||
if diff := cmp.Diff( | ||
resp.Msg, tt.wants, | ||
cmpopts.IgnoreUnexported( | ||
adminv1.CreateInvitationCodeResponse{}, | ||
adminv1.InvitationCode{}, | ||
timestamppb.Timestamp{}), | ||
cmpopts.IgnoreFields(adminv1.CreateInvitationCodeResponse{}, "InvitationCode.Code"), | ||
); diff != "" { | ||
t.Errorf("(-got, +want)\n%s", diff) | ||
} | ||
}) | ||
} | ||
} |
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
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,12 @@ | ||
package domain | ||
|
||
import "time" | ||
|
||
type Clock func() time.Time | ||
|
||
func (c Clock) Now() time.Time { | ||
if c == nil { | ||
return time.Now() | ||
} | ||
return c() | ||
} |
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
Oops, something went wrong.