Skip to content

Commit

Permalink
qrg: Fix linter errors
Browse files Browse the repository at this point in the history
  • Loading branch information
Vlad Vitan committed Sep 4, 2024
1 parent 41f60ae commit 84deef9
Show file tree
Hide file tree
Showing 8 changed files with 55 additions and 21 deletions.
2 changes: 1 addition & 1 deletion pkg/deviceclaimingserver/grpc_gateways_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ var (
authorizedCallOpt = grpc.PerRPCCredentials(authorizedMD)
)

func TestGatewayClaimingServer(t *testing.T) { // nolint:paralleltest
func TestGatewayClaimingServer(t *testing.T) { //nolint:paralleltest
a := assertions.New(t)
ctx := log.NewContext(test.Context(), test.GetLogger(t))
ctx, cancelCtx := context.WithCancel(ctx)
Expand Down
10 changes: 8 additions & 2 deletions pkg/qrcodegenerator/grpc_gateways.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,10 @@ type gatewayQRCodeGeneratorServer struct {
}

// GetFormat implements EndDeviceQRCodeGenerator.
func (s *gatewayQRCodeGeneratorServer) GetFormat(ctx context.Context, req *ttnpb.GetQRCodeFormatRequest) (*ttnpb.QRCodeFormat, error) {
func (s *gatewayQRCodeGeneratorServer) GetFormat(
ctx context.Context,
req *ttnpb.GetQRCodeFormatRequest,
) (*ttnpb.QRCodeFormat, error) {
_, err := rpcmetadata.WithForwardedAuth(ctx, s.QRG.AllowInsecureForCredentials())
if err != nil {
return nil, err
Expand All @@ -41,7 +44,10 @@ func (s *gatewayQRCodeGeneratorServer) GetFormat(ctx context.Context, req *ttnpb
}

// Parse implements EndDeviceQRCodeGenerator.
func (s *gatewayQRCodeGeneratorServer) Parse(ctx context.Context, req *ttnpb.ParseGatewayQRCodeRequest) (*ttnpb.ParseGatewayQRCodeResponse, error) {
func (s *gatewayQRCodeGeneratorServer) Parse(
ctx context.Context,
req *ttnpb.ParseGatewayQRCodeRequest,
) (*ttnpb.ParseGatewayQRCodeResponse, error) {
_, err := rpcmetadata.WithForwardedAuth(ctx, s.QRG.AllowInsecureForCredentials())
if err != nil {
return nil, err
Expand Down
15 changes: 12 additions & 3 deletions pkg/qrcodegenerator/grpc_gateways_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,24 +22,30 @@ import (
componenttest "go.thethings.network/lorawan-stack/v3/pkg/component/test"
"go.thethings.network/lorawan-stack/v3/pkg/errors"
"go.thethings.network/lorawan-stack/v3/pkg/log"
. "go.thethings.network/lorawan-stack/v3/pkg/qrcodegenerator"
"go.thethings.network/lorawan-stack/v3/pkg/qrcodegenerator"

"go.thethings.network/lorawan-stack/v3/pkg/qrcodegenerator/qrcode/gateways"
"go.thethings.network/lorawan-stack/v3/pkg/ttnpb"
"go.thethings.network/lorawan-stack/v3/pkg/util/test"
"go.thethings.network/lorawan-stack/v3/pkg/util/test/assertions/should"
)

func TestGatewayQRCodeParsing(t *testing.T) {
t.Parallel()

a := assertions.New(t)
ctx := log.NewContext(test.Context(), test.GetLogger(t))

c := componenttest.NewComponent(t, &component.Config{})
ttigpro1 := new(gateways.TTIGPRO1Format)
qrg, err := New(c, &Config{}, WithGatewayFormat(ttigpro1.ID(), ttigpro1))
qrg, err := qrcodegenerator.New(c,
&qrcodegenerator.Config{},
qrcodegenerator.WithGatewayFormat(ttigpro1.ID(), ttigpro1),
)
test.Must(qrg, err)

componenttest.StartComponent(t, c)
defer c.Close()
t.Cleanup(func() { c.Close() })

mustHavePeer(ctx, c, ttnpb.ClusterRole_QR_CODE_GENERATOR)

Expand Down Expand Up @@ -117,7 +123,10 @@ func TestGatewayQRCodeParsing(t *testing.T) {
},
},
} {
tc := tc
t.Run(tc.Name, func(t *testing.T) {
t.Parallel()

resp, err := client.Parse(ctx, &ttnpb.ParseGatewayQRCodeRequest{
FormatId: tc.FormatID,
QrCode: tc.GetQRData(),
Expand Down
5 changes: 3 additions & 2 deletions pkg/qrcodegenerator/qrcode/gateways/gateways.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.

// Package gateways provides a QR code parser for gateways.
package gateways

import (
Expand Down Expand Up @@ -55,13 +56,13 @@ type Server struct {
}

// New returns a new Server.
func New(ctx context.Context) *Server {
func New(_ context.Context) *Server {
s := &Server{
// Newer formats should be added to this slice first to
// preferentially match with those first.
gatewayFormats: []gatewayFormat{
{
id: formatIDttigpro1,
id: formatIDTTIGPRO1,
format: new(TTIGPRO1Format),
},
},
Expand Down
9 changes: 7 additions & 2 deletions pkg/qrcodegenerator/qrcode/gateways/gateways_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,14 @@ import (
"testing"

"github.com/smarty/assertions"
. "go.thethings.network/lorawan-stack/v3/pkg/qrcodegenerator/qrcode/gateways"
"go.thethings.network/lorawan-stack/v3/pkg/qrcodegenerator/qrcode/gateways"
"go.thethings.network/lorawan-stack/v3/pkg/util/test"
"go.thethings.network/lorawan-stack/v3/pkg/util/test/assertions/should"
)

func TestParseGatewaysAuthenticationCodes(t *testing.T) {
t.Parallel()

for i, tc := range []struct {
FormatID string
Data []byte
Expand All @@ -39,10 +41,13 @@ func TestParseGatewaysAuthenticationCodes(t *testing.T) {
ExpectedOwnerToken: "abcdef123456",
},
} {
tc := tc

t.Run(strconv.Itoa(i), func(t *testing.T) {
t.Parallel()
a := assertions.New(t)

qrCode := New(context.Background())
qrCode := gateways.New(context.Background())

d, err := qrCode.Parse(tc.FormatID, tc.Data)
data := test.Must(d, err)
Expand Down
14 changes: 7 additions & 7 deletions pkg/qrcodegenerator/qrcode/gateways/ttigpro1.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import (
)

const (
formatIDttigpro1 = "ttigpro1"
formatIDTTIGPRO1 = "ttigpro1"

euiLength = 16
ownerTokenLength = 12
Expand All @@ -35,7 +35,7 @@ type ttigpro1 struct {
}

// ttigpro1Regex is the regular expression to match the TTIGPRO1 format.
// The format is as follows: https://ttig.pro/c/{16 lowercase base16 chars}/{12 base62 chars}
// The format is as follows: https://ttig.pro/c/{16 lowercase base16 chars}/{12 base62 chars}.
var ttigpro1Regex = regexp.MustCompile(`^https://ttig\.pro/c/([a-f0-9]{16})/([a-z0-9]{12})$`)

// UnmarshalText implements the TextUnmarshaler interface.
Expand All @@ -60,16 +60,16 @@ func (m *ttigpro1) UnmarshalText(text []byte) error {
}

// FormatID implements the Data interface.
func (m *ttigpro1) FormatID() string {
return formatIDttigpro1
func (*ttigpro1) FormatID() string {
return formatIDTTIGPRO1
}

func (m *ttigpro1) GatewayEUI() types.EUI64 {
return m.gatewayEUI
}

func (m *ttigpro1) OwnerToken() string {
return string(m.ownerToken)
return m.ownerToken
}

// TTIGPRO1Format implements the TTIGPRO1 Format.
Expand All @@ -79,7 +79,7 @@ type TTIGPRO1Format struct{}
func (TTIGPRO1Format) Format() *ttnpb.QRCodeFormat {
return &ttnpb.QRCodeFormat{
Name: "TTIGPRO1",
Description: "TTI QR code format for gateway devices.",
Description: "QR code format for The Things Indoor Gateway Pro.",
FieldMask: ttnpb.FieldMask(
"ids.eui",
"claim_authentication_code.secret.value",
Expand All @@ -89,7 +89,7 @@ func (TTIGPRO1Format) Format() *ttnpb.QRCodeFormat {

// ID is the identifier of the format as a string.
func (TTIGPRO1Format) ID() string {
return formatIDttigpro1
return formatIDTTIGPRO1
}

// New implements the Format interface.
Expand Down
13 changes: 13 additions & 0 deletions pkg/qrcodegenerator/qrcode/gateways/ttigpro1_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,11 @@ import (
)

func TestTTIGPRO1(t *testing.T) {
t.Parallel()

t.Run("Decode", func(t *testing.T) {
t.Parallel()

for _, tc := range []struct {
Name string
Data []byte
Expand All @@ -43,46 +47,55 @@ func TestTTIGPRO1(t *testing.T) {
Name: "InvalidURLPrefix",
Data: []byte("https://example.com/c/ec656efffe000128/abcdef12"),
ErrorAssertion: func(t *testing.T, err error) bool {
t.Helper()
return assertions.New(t).So(errors.IsInvalidArgument(err), should.BeTrue)
},
},
{
Name: "Invalid/EUINotLowercase",
Data: []byte("https://ttig.pro/c/EC656effFe000128/abcdef12"),
ErrorAssertion: func(t *testing.T, err error) bool {
t.Helper()
return assertions.New(t).So(errors.IsInvalidArgument(err), should.BeTrue)
},
},
{
Name: "Invalid/EUILength",
Data: []byte("https://ttig.pro/c/ec656efffe00012/abcdef12"),
ErrorAssertion: func(t *testing.T, err error) bool {
t.Helper()
return assertions.New(t).So(errors.IsInvalidArgument(err), should.BeTrue)
},
},
{
Name: "Invalid/EUINotBase16",
Data: []byte("https://ttig.pro/c/ec656efffe00012g/abcdef12"),
ErrorAssertion: func(t *testing.T, err error) bool {
t.Helper()
return assertions.New(t).So(errors.IsInvalidArgument(err), should.BeTrue)
},
},
{
Name: "Invalid/OwnerTokenLength",
Data: []byte("https://ttig.pro/c/ec656efffe000128/abcdef123"),
ErrorAssertion: func(t *testing.T, err error) bool {
t.Helper()
return assertions.New(t).So(errors.IsInvalidArgument(err), should.BeTrue)
},
},
{
Name: "Invalid/OwnerTokenNotBase62",
Data: []byte("https://ttig.pro/c/ec656efffe000128/abcdef12!"),
ErrorAssertion: func(t *testing.T, err error) bool {
t.Helper()
return assertions.New(t).So(errors.IsInvalidArgument(err), should.BeTrue)
},
},
} {
tc := tc
t.Run(tc.Name, func(t *testing.T) {
t.Parallel()

a := assertions.New(t)

var data ttigpro1
Expand Down
8 changes: 4 additions & 4 deletions pkg/qrcodegenerator/qrcodegenerator.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ type QRCodeGenerator struct {
var errFormatNotFound = errors.DefineNotFound("format_not_found", "format `{id}` not found")

// New returns a new *QRCodeGenerator.
func New(c *component.Component, conf *Config, opts ...Option) (*QRCodeGenerator, error) {
func New(c *component.Component, _ *Config, opts ...Option) (*QRCodeGenerator, error) {
ctx := log.NewContextWithField(c.Context(), "namespace", "qrcodegenerator")
qrg := &QRCodeGenerator{
Component: c,
Expand Down Expand Up @@ -91,7 +91,7 @@ func (qrg *QRCodeGenerator) Context() context.Context {
}

// Roles returns the roles that the QR Code Generator fulfills.
func (qrg *QRCodeGenerator) Roles() []ttnpb.ClusterRole {
func (*QRCodeGenerator) Roles() []ttnpb.ClusterRole {
return []ttnpb.ClusterRole{ttnpb.ClusterRole_QR_CODE_GENERATOR}
}

Expand All @@ -103,6 +103,6 @@ func (qrg *QRCodeGenerator) RegisterServices(s *grpc.Server) {

// RegisterHandlers registers gRPC handlers.
func (qrg *QRCodeGenerator) RegisterHandlers(s *runtime.ServeMux, conn *grpc.ClientConn) {
ttnpb.RegisterEndDeviceQRCodeGeneratorHandler(qrg.Context(), s, conn)
ttnpb.RegisterGatewayQRCodeGeneratorHandler(qrg.Context(), s, conn)
ttnpb.RegisterEndDeviceQRCodeGeneratorHandler(qrg.Context(), s, conn) //nolint:errcheck
ttnpb.RegisterGatewayQRCodeGeneratorHandler(qrg.Context(), s, conn) //nolint:errcheck
}

0 comments on commit 84deef9

Please sign in to comment.