|
| 1 | +/* |
| 2 | +Copyright 2024 The Dapr Authors |
| 3 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +you may not use this file except in compliance with the License. |
| 5 | +You may obtain a copy of the License at |
| 6 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 7 | +Unless required by applicable law or agreed to in writing, software |
| 8 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 9 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implieh. |
| 10 | +See the License for the specific language governing permissions and |
| 11 | +limitations under the License. |
| 12 | +*/ |
| 13 | + |
| 14 | +package appapitoken |
| 15 | + |
| 16 | +import ( |
| 17 | + "context" |
| 18 | + "testing" |
| 19 | + "time" |
| 20 | + |
| 21 | + "github.com/stretchr/testify/assert" |
| 22 | + "github.com/stretchr/testify/require" |
| 23 | + "google.golang.org/grpc/metadata" |
| 24 | + "google.golang.org/protobuf/types/known/anypb" |
| 25 | + |
| 26 | + commonv1 "github.com/dapr/dapr/pkg/proto/common/v1" |
| 27 | + runtimev1 "github.com/dapr/dapr/pkg/proto/runtime/v1" |
| 28 | + "github.com/dapr/dapr/tests/integration/framework" |
| 29 | + "github.com/dapr/dapr/tests/integration/framework/process/daprd" |
| 30 | + "github.com/dapr/dapr/tests/integration/framework/process/grpc/app" |
| 31 | + "github.com/dapr/dapr/tests/integration/suite" |
| 32 | + testpb "github.com/dapr/dapr/tests/integration/suite/daprd/serviceinvocation/grpc/proto" |
| 33 | +) |
| 34 | + |
| 35 | +func init() { |
| 36 | + suite.Register(new(remotebothtokens)) |
| 37 | +} |
| 38 | + |
| 39 | +type remotebothtokens struct { |
| 40 | + daprd1 *daprd.Daprd |
| 41 | + daprd2 *daprd.Daprd |
| 42 | + ch chan metadata.MD |
| 43 | +} |
| 44 | + |
| 45 | +func (b *remotebothtokens) Setup(t *testing.T) []framework.Option { |
| 46 | + fn, ch := newServer() |
| 47 | + b.ch = ch |
| 48 | + app := app.New(t, |
| 49 | + app.WithRegister(fn), |
| 50 | + app.WithOnInvokeFn(func(ctx context.Context, _ *commonv1.InvokeRequest) (*commonv1.InvokeResponse, error) { |
| 51 | + md, ok := metadata.FromIncomingContext(ctx) |
| 52 | + require.True(t, ok) |
| 53 | + b.ch <- md |
| 54 | + return new(commonv1.InvokeResponse), nil |
| 55 | + }), |
| 56 | + ) |
| 57 | + |
| 58 | + b.daprd1 = daprd.New(t, |
| 59 | + daprd.WithAppID("app1"), |
| 60 | + daprd.WithAppProtocol("grpc"), |
| 61 | + daprd.WithAppAPIToken(t, "abc"), |
| 62 | + ) |
| 63 | + |
| 64 | + b.daprd2 = daprd.New(t, |
| 65 | + daprd.WithAppProtocol("grpc"), |
| 66 | + daprd.WithAppAPIToken(t, "def"), |
| 67 | + daprd.WithAppPort(app.Port(t)), |
| 68 | + ) |
| 69 | + |
| 70 | + return []framework.Option{ |
| 71 | + framework.WithProcesses(app, b.daprd1, b.daprd2), |
| 72 | + } |
| 73 | +} |
| 74 | + |
| 75 | +func (b *remotebothtokens) Run(t *testing.T, ctx context.Context) { |
| 76 | + b.daprd1.WaitUntilRunning(t, ctx) |
| 77 | + b.daprd2.WaitUntilRunning(t, ctx) |
| 78 | + |
| 79 | + client := testpb.NewTestServiceClient(b.daprd1.GRPCConn(t, ctx)) |
| 80 | + ctx = metadata.AppendToOutgoingContext(ctx, "dapr-app-id", b.daprd2.AppID()) |
| 81 | + _, err := client.Ping(ctx, new(testpb.PingRequest)) |
| 82 | + require.NoError(t, err) |
| 83 | + |
| 84 | + select { |
| 85 | + case md := <-b.ch: |
| 86 | + require.Equal(t, []string{"def"}, md.Get("dapr-api-token")) |
| 87 | + case <-time.After(10 * time.Second): |
| 88 | + assert.Fail(t, "timed out waiting for metadata") |
| 89 | + } |
| 90 | + |
| 91 | + dclient := b.daprd1.GRPCClient(t, ctx) |
| 92 | + _, err = dclient.InvokeService(ctx, &runtimev1.InvokeServiceRequest{ |
| 93 | + Id: b.daprd2.AppID(), |
| 94 | + Message: &commonv1.InvokeRequest{ |
| 95 | + Method: "helloworld", |
| 96 | + Data: new(anypb.Any), |
| 97 | + HttpExtension: &commonv1.HTTPExtension{Verb: commonv1.HTTPExtension_GET}, |
| 98 | + }, |
| 99 | + }) |
| 100 | + require.NoError(t, err) |
| 101 | + |
| 102 | + select { |
| 103 | + case md := <-b.ch: |
| 104 | + require.Equal(t, []string{"def"}, md.Get("dapr-api-token")) |
| 105 | + case <-time.After(5 * time.Second): |
| 106 | + assert.Fail(t, "timed out waiting for metadata") |
| 107 | + } |
| 108 | +} |
0 commit comments