Skip to content

Commit

Permalink
coap-gateway: handle proper temporary error
Browse files Browse the repository at this point in the history
  • Loading branch information
jkralik committed Nov 29, 2022
1 parent 505ad26 commit f1b69ac
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 3 deletions.
42 changes: 42 additions & 0 deletions coap-gateway/service/refreshToken_test.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,20 @@
package service_test

import (
"context"
"testing"
"time"

coapCodes "github.com/plgd-dev/go-coap/v3/message/codes"
coapgwTest "github.com/plgd-dev/hub/v2/coap-gateway/test"
"github.com/plgd-dev/hub/v2/coap-gateway/uri"
"github.com/plgd-dev/hub/v2/pkg/fsnotify"
"github.com/plgd-dev/hub/v2/pkg/log"
"github.com/plgd-dev/hub/v2/pkg/net/listener"
"github.com/plgd-dev/hub/v2/test/config"
oauthTest "github.com/plgd-dev/hub/v2/test/oauth-server/test"
testService "github.com/plgd-dev/hub/v2/test/service"
"github.com/stretchr/testify/require"
)

type TestCoapRefreshTokenResponse struct {
Expand Down Expand Up @@ -77,3 +84,38 @@ func TestRefreshTokenHandlerWithRetry(t *testing.T) {
time.Sleep(time.Second)
}
}

func TestRefreshTokenWithOAuthNotWorking(t *testing.T) {
test := testEl{"ServiceUnavailable", input{coapCodes.POST, `{"di": "` + CertIdentity + `", "uid":"` + AuthorizationUserID + `", "refreshtoken":"refresh-token"}`, nil}, output{coapCodes.ServiceUnavailable, `temporary error`, nil}, true}
ctx, cancel := context.WithTimeout(context.Background(), time.Second*30)
defer cancel()
cfg := oauthTest.MakeConfig(t)
oauthShutdown := oauthTest.New(t, cfg)
coapgwCfg := coapgwTest.MakeConfig(t)
coapgwCfg.APIs.COAP.Authorization.Providers[0].HTTP.Timeout = time.Second
shutdown := testService.SetUpServices(ctx, t, testService.SetUpServicesId|testService.SetUpServicesCoapGateway|testService.SetUpServicesResourceAggregate|testService.SetUpServicesResourceDirectory, testService.WithCOAPGWConfig(coapgwCfg))
defer shutdown()

co := testCoapDial(t, "", true, time.Now().Add(time.Minute))
if co == nil {
return
}
testSignUp(t, CertIdentity, co)
defer func() {
_ = co.Close()
}()
oauthShutdown()
fileWatcher, err := fsnotify.NewWatcher()
require.NoError(t, err)
defer func() {
err = fileWatcher.Close()
require.NoError(t, err)
}()
s, err := listener.New(config.MakeListenerConfig(cfg.APIs.HTTP.Connection.Addr), fileWatcher, log.Get())
require.NoError(t, err)
defer func() {
err = s.Close()
require.NoError(t, err)
}()
testPostHandler(t, uri.RefreshToken, test, co)
}
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ require (
github.com/pion/dtls/v2 v2.1.6-0.20221001075407-984d41b9981a
github.com/pion/logging v0.2.2
github.com/plgd-dev/device/v2 v2.0.1
github.com/plgd-dev/go-coap/v3 v3.0.1-0.20221107080825-5cfe76a3720f
github.com/plgd-dev/go-coap/v3 v3.0.1
github.com/plgd-dev/kit/v2 v2.0.0-20211006190727-057b33161b90
github.com/sirupsen/logrus v1.9.0
github.com/stretchr/testify v1.8.1
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -302,8 +302,8 @@ github.com/plgd-dev/device/v2 v2.0.1 h1:97LZZD613yzjnzqzU811d0wwKOAiMopvV6RrSo3v
github.com/plgd-dev/device/v2 v2.0.1/go.mod h1:+zgZnklgQ36DSalNUnm0iioVZhoYV2HRCMDtoOBo3zI=
github.com/plgd-dev/go-coap/v2 v2.0.4-0.20200819112225-8eb712b901bc/go.mod h1:+tCi9Q78H/orWRtpVWyBgrr4vKFo2zYtbbxUllerBp4=
github.com/plgd-dev/go-coap/v2 v2.4.1-0.20210517130748-95c37ac8e1fa/go.mod h1:rA7fc7ar+B/qa+Q0hRqv7yj/EMtIlmo1l7vkQGSrHPU=
github.com/plgd-dev/go-coap/v3 v3.0.1-0.20221107080825-5cfe76a3720f h1:o6RSvRiTeGso9L8OlRZEzBrp+Y3SHUQ28Nu4GuL+O8Q=
github.com/plgd-dev/go-coap/v3 v3.0.1-0.20221107080825-5cfe76a3720f/go.mod h1:PzBLbPu2+gmC+eHkCCozZr3WfA3bC0tM/6IuXodvjZE=
github.com/plgd-dev/go-coap/v3 v3.0.1 h1:dJjaTnfGKp6nY7VeD3Vj03PMgBlz46/ZyinsDpatu/U=
github.com/plgd-dev/go-coap/v3 v3.0.1/go.mod h1:PzBLbPu2+gmC+eHkCCozZr3WfA3bC0tM/6IuXodvjZE=
github.com/plgd-dev/kit v0.0.0-20200819113605-d5fcf3e94f63/go.mod h1:Yl9zisyXfPdtP9hTWlJqjJYXmgU/jtSDKttz9/CeD90=
github.com/plgd-dev/kit/v2 v2.0.0-20211006190727-057b33161b90 h1:TC1HJ/UbyflJFPvaOdGmNZ5TeFGex1/dyr9urNGLy7M=
github.com/plgd-dev/kit/v2 v2.0.0-20211006190727-057b33161b90/go.mod h1:Z7oKFLSGQjdi8eInxwFCs0tSApuEM1o0qNck+sJYp4M=
Expand Down

0 comments on commit f1b69ac

Please sign in to comment.