refactor: monitor add - new endpoint for checking tribe tags (#40)
All checks were successful
continuous-integration/drone/push Build is passing
continuous-integration/drone/tag Build is passing

Reviewed-on: #40
This commit is contained in:
Dawid Wysokiński 2022-11-10 11:13:16 +00:00
parent 91d2405912
commit 661f26ecea
5 changed files with 113 additions and 95 deletions

View File

@ -76,8 +76,14 @@ func (m *Monitor) Create(ctx context.Context, groupID, serverID, tribeTag string
}
}
// check if tribe exists
tribe, err := m.client.GetTribeByTag(ctx, group.VersionCode, group.ServerKey, tribeTag)
tribes, err := m.client.ListTribes(ctx, group.VersionCode, group.ServerKey, twhelp.ListTribesQueryParams{
Limit: 1,
Tags: []string{tribeTag},
Deleted: twhelp.NullBool{
Valid: true,
Bool: false,
},
})
if err != nil {
var apiErr twhelp.APIError
if !errors.As(err, &apiErr) {
@ -87,12 +93,12 @@ func (m *Monitor) Create(ctx context.Context, groupID, serverID, tribeTag string
Tag: tribeTag,
}
}
if !tribe.DeletedAt.IsZero() {
if len(tribes) == 0 {
return domain.Monitor{}, domain.TribeDoesNotExistError{
Tag: tribeTag,
}
}
tribe := tribes[0]
params, err := domain.NewCreateMonitorParams(group.ID, tribe.ID)
if err != nil {

View File

@ -53,7 +53,7 @@ func TestMonitor_Create(t *testing.T) {
ProfileURL: "https://pl151.plemiona.pl/game.php?screen=info_player&id=150",
DeletedAt: time.Time{},
}
client.GetTribeByTagReturns(tribe, nil)
client.ListTribesReturns([]twhelp.Tribe{tribe}, nil)
groupID := uuid.NewString()
@ -114,85 +114,65 @@ func TestMonitor_Create(t *testing.T) {
t.Run("ERR: tribe doesn't exist", func(t *testing.T) {
t.Parallel()
t.Run("API error", func(t *testing.T) {
t.Parallel()
tests := []struct {
name string
err error
}{
{
name: "err=null",
},
{
name: "err!=null",
err: twhelp.APIError{
Code: twhelp.ErrorCodeInternalServerError,
Message: "internal server error",
},
},
}
repo := &mock.FakeMonitorRepository{}
repo.ListReturns(nil, nil)
repo.CreateCalls(func(ctx context.Context, params domain.CreateMonitorParams) (domain.Monitor, error) {
return domain.Monitor{
ID: uuid.NewString(),
TribeID: params.TribeID(),
GroupID: params.GroupID(),
CreatedAt: time.Now(),
}, nil
for _, tt := range tests {
tt := tt
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
repo := &mock.FakeMonitorRepository{}
repo.ListReturns(nil, nil)
repo.CreateCalls(func(ctx context.Context, params domain.CreateMonitorParams) (domain.Monitor, error) {
return domain.Monitor{
ID: uuid.NewString(),
TribeID: params.TribeID(),
GroupID: params.GroupID(),
CreatedAt: time.Now(),
}, nil
})
groupSvc := &mock.FakeGroupReader{}
groupSvc.GetCalls(func(ctx context.Context, groupID, serverID string) (domain.Group, error) {
return domain.Group{
ID: groupID,
ServerID: serverID,
ChannelGains: "",
ChannelLosses: "",
ServerKey: "pl151",
VersionCode: "pl",
CreatedAt: time.Now(),
}, nil
})
client := &mock.FakeTWHelpClient{}
client.ListTribesReturns(nil, tt.err)
tag := "TAG"
monitor, err := service.NewMonitor(repo, groupSvc, client, zap.NewNop(), 10).
Create(context.Background(), uuid.NewString(), uuid.NewString(), tag)
assert.ErrorIs(t, err, domain.TribeDoesNotExistError{
Tag: tag,
})
assert.Zero(t, monitor)
})
groupSvc := &mock.FakeGroupReader{}
groupSvc.GetCalls(func(ctx context.Context, groupID, serverID string) (domain.Group, error) {
return domain.Group{
ID: groupID,
ServerID: serverID,
ChannelGains: "",
ChannelLosses: "",
ServerKey: "pl151",
VersionCode: "pl",
CreatedAt: time.Now(),
}, nil
})
client := &mock.FakeTWHelpClient{}
client.GetTribeByTagReturns(twhelp.Tribe{}, twhelp.APIError{
Code: twhelp.ErrorCodeEntityNotFound,
Message: "tribe not found",
})
tag := "TAG"
monitor, err := service.NewMonitor(repo, groupSvc, client, zap.NewNop(), 10).
Create(context.Background(), uuid.NewString(), uuid.NewString(), tag)
assert.ErrorIs(t, err, domain.TribeDoesNotExistError{
Tag: tag,
})
assert.Zero(t, monitor)
})
t.Run("tribe is deleted", func(t *testing.T) {
t.Parallel()
repo := &mock.FakeMonitorRepository{}
repo.ListReturns(nil, nil)
groupSvc := &mock.FakeGroupReader{}
groupSvc.GetCalls(func(ctx context.Context, groupID, serverID string) (domain.Group, error) {
return domain.Group{
ID: groupID,
ServerID: serverID,
ChannelGains: "",
ChannelLosses: "",
ServerKey: "pl151",
VersionCode: "pl",
CreatedAt: time.Now(),
}, nil
})
client := &mock.FakeTWHelpClient{}
tribe := twhelp.Tribe{
ID: 150,
Tag: "*TAG*",
Name: uuid.NewString(),
ProfileURL: "https://pl151.plemiona.pl/game.php?screen=info_player&id=150",
DeletedAt: time.Now(),
}
client.GetTribeByTagReturns(tribe, nil)
monitor, err := service.NewMonitor(repo, groupSvc, client, zap.NewNop(), 10).
Create(context.Background(), uuid.NewString(), uuid.NewString(), tribe.Tag)
assert.ErrorIs(t, err, domain.TribeDoesNotExistError{
Tag: tribe.Tag,
})
assert.Zero(t, monitor)
})
}
})
}

View File

@ -11,14 +11,10 @@ import (
//counterfeiter:generate -o internal/mock/twhelp_client.gen.go . TWHelpClient
type TWHelpClient interface {
ListVersions(ctx context.Context) ([]twhelp.Version, error)
ListServers(
ctx context.Context,
version string,
params twhelp.ListServersQueryParams,
) ([]twhelp.Server, error)
ListServers(ctx context.Context, version string, params twhelp.ListServersQueryParams) ([]twhelp.Server, error)
GetServer(ctx context.Context, version, server string) (twhelp.Server, error)
ListTribes(ctx context.Context, version, server string, params twhelp.ListTribesQueryParams) ([]twhelp.Tribe, error)
GetTribeByID(ctx context.Context, version, server string, id int64) (twhelp.Tribe, error)
GetTribeByTag(ctx context.Context, version, server, tag string) (twhelp.Tribe, error)
ListEnnoblements(
ctx context.Context,
version, server string,

View File

@ -18,8 +18,8 @@ const (
endpointListVersions = "/api/v1/versions"
endpointListServers = "/api/v1/versions/%s/servers"
endpointGetServer = "/api/v1/versions/%s/servers/%s"
endpointListTribes = "/api/v1/versions/%s/servers/%s/tribes"
endpointGetTribeByID = "/api/v1/versions/%s/servers/%s/tribes/%d"
endpointGetTribeByTag = "/api/v1/versions/%s/servers/%s/tribes/tag/%s"
endpointListEnnoblements = "/api/v1/versions/%s/servers/%s/ennoblements"
)
@ -106,17 +106,49 @@ func (c *Client) GetServer(ctx context.Context, version, server string) (Server,
return resp.Data, nil
}
func (c *Client) GetTribeByID(ctx context.Context, version, server string, id int64) (Tribe, error) {
var resp getTribeResp
if err := c.getJSON(ctx, fmt.Sprintf(endpointGetTribeByID, version, server, id), &resp); err != nil {
return Tribe{}, err
type ListTribesQueryParams struct {
Limit int32
Offset int32
Tags []string
Deleted NullBool
}
func (c *Client) ListTribes(
ctx context.Context,
version, server string,
params ListTribesQueryParams,
) ([]Tribe, error) {
q := url.Values{}
if params.Limit > 0 {
q.Set("limit", strconv.Itoa(int(params.Limit)))
}
if params.Offset > 0 {
q.Set("offset", strconv.Itoa(int(params.Offset)))
}
if params.Deleted.Valid {
q.Set("deleted", strconv.FormatBool(params.Deleted.Bool))
}
if params.Tags != nil {
for _, t := range params.Tags {
q.Add("tag", t)
}
}
var resp listTribesResp
if err := c.getJSON(ctx, fmt.Sprintf(endpointListTribes, version, server)+"?"+q.Encode(), &resp); err != nil {
return nil, err
}
return resp.Data, nil
}
func (c *Client) GetTribeByTag(ctx context.Context, version, server, tag string) (Tribe, error) {
func (c *Client) GetTribeByID(ctx context.Context, version, server string, id int64) (Tribe, error) {
var resp getTribeResp
if err := c.getJSON(ctx, fmt.Sprintf(endpointGetTribeByTag, version, server, tag), &resp); err != nil {
if err := c.getJSON(ctx, fmt.Sprintf(endpointGetTribeByID, version, server, id), &resp); err != nil {
return Tribe{}, err
}
return resp.Data, nil

View File

@ -65,6 +65,10 @@ type Tribe struct {
DeletedAt time.Time `json:"deletedAt"`
}
type listTribesResp struct {
Data []Tribe `json:"data"`
}
type getTribeResp struct {
Data Tribe `json:"data"`
}