|
| 1 | +package module_test |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/json" |
| 5 | + "testing" |
| 6 | + |
| 7 | + "github.com/stretchr/testify/require" |
| 8 | + sha256_verifier "github.com/wundergraph/cosmo/router-tests/modules/sha256-verifier" |
| 9 | + "github.com/wundergraph/cosmo/router-tests/testenv" |
| 10 | + "github.com/wundergraph/cosmo/router/core" |
| 11 | + "github.com/wundergraph/cosmo/router/pkg/config" |
| 12 | +) |
| 13 | + |
| 14 | +func TestSha256VerifierModule(t *testing.T) { |
| 15 | + t.Parallel() |
| 16 | + |
| 17 | + t.Run("verify Sha256Hash is not captured when sha256 force is not enabled", func(t *testing.T) { |
| 18 | + t.Parallel() |
| 19 | + |
| 20 | + resultContainer := &sha256_verifier.ResultContainer{} |
| 21 | + |
| 22 | + cfg := config.Config{ |
| 23 | + Graph: config.Graph{}, |
| 24 | + Modules: map[string]interface{}{ |
| 25 | + "sha256VerifierModule": sha256_verifier.Sha256VerifierModule{ |
| 26 | + ForceSha256: false, |
| 27 | + ResultContainer: resultContainer, |
| 28 | + }, |
| 29 | + }, |
| 30 | + } |
| 31 | + |
| 32 | + testenv.Run(t, &testenv.Config{ |
| 33 | + RouterOptions: []core.Option{ |
| 34 | + core.WithModulesConfig(cfg.Modules), |
| 35 | + core.WithCustomModules(&sha256_verifier.Sha256VerifierModule{}), |
| 36 | + }, |
| 37 | + }, func(t *testing.T, xEnv *testenv.Environment) { |
| 38 | + res, err := xEnv.MakeGraphQLRequest(testenv.GraphQLRequest{ |
| 39 | + Query: `query MyQuery { employees { id } }`, |
| 40 | + OperationName: json.RawMessage(`"MyQuery"`), |
| 41 | + }) |
| 42 | + require.NoError(t, err) |
| 43 | + require.Equal(t, 200, res.Response.StatusCode) |
| 44 | + |
| 45 | + require.Empty(t, resultContainer.Sha256Result) |
| 46 | + }) |
| 47 | + }) |
| 48 | + |
| 49 | + t.Run("verify sha256Hash is captured from operation when force is enabled", func(t *testing.T) { |
| 50 | + t.Parallel() |
| 51 | + |
| 52 | + resultContainer := &sha256_verifier.ResultContainer{} |
| 53 | + |
| 54 | + cfg := config.Config{ |
| 55 | + Graph: config.Graph{}, |
| 56 | + Modules: map[string]interface{}{ |
| 57 | + "sha256VerifierModule": sha256_verifier.Sha256VerifierModule{ |
| 58 | + ForceSha256: true, |
| 59 | + ResultContainer: resultContainer, |
| 60 | + }, |
| 61 | + }, |
| 62 | + } |
| 63 | + |
| 64 | + testenv.Run(t, &testenv.Config{ |
| 65 | + RouterOptions: []core.Option{ |
| 66 | + core.WithModulesConfig(cfg.Modules), |
| 67 | + core.WithCustomModules(&sha256_verifier.Sha256VerifierModule{}), |
| 68 | + }, |
| 69 | + }, func(t *testing.T, xEnv *testenv.Environment) { |
| 70 | + res, err := xEnv.MakeGraphQLRequest(testenv.GraphQLRequest{ |
| 71 | + Query: `query MyQuery { employees { id } }`, |
| 72 | + OperationName: json.RawMessage(`"MyQuery"`), |
| 73 | + }) |
| 74 | + require.NoError(t, err) |
| 75 | + require.Equal(t, 200, res.Response.StatusCode) |
| 76 | + |
| 77 | + require.NotEmpty(t, resultContainer.Sha256Result) |
| 78 | + require.Equal(t, "f037469b9c85bb28ae4c13e1d51c1f7e3333ecbe3c28b877c8659a52378f56c0", resultContainer.Sha256Result) |
| 79 | + }) |
| 80 | + }) |
| 81 | + |
| 82 | + t.Run("verify different queries produces different Sha256Hashes", func(t *testing.T) { |
| 83 | + t.Parallel() |
| 84 | + |
| 85 | + resultContainer := &sha256_verifier.ResultContainer{} |
| 86 | + |
| 87 | + cfg := config.Config{ |
| 88 | + Graph: config.Graph{}, |
| 89 | + Modules: map[string]interface{}{ |
| 90 | + "sha256VerifierModule": sha256_verifier.Sha256VerifierModule{ |
| 91 | + ForceSha256: true, |
| 92 | + ResultContainer: resultContainer, |
| 93 | + }, |
| 94 | + }, |
| 95 | + } |
| 96 | + |
| 97 | + testenv.Run(t, &testenv.Config{ |
| 98 | + RouterOptions: []core.Option{ |
| 99 | + core.WithModulesConfig(cfg.Modules), |
| 100 | + core.WithCustomModules(&sha256_verifier.Sha256VerifierModule{}), |
| 101 | + }, |
| 102 | + }, func(t *testing.T, xEnv *testenv.Environment) { |
| 103 | + _, err := xEnv.MakeGraphQLRequest(testenv.GraphQLRequest{ |
| 104 | + Query: `query ConsistentQuery { employees { id } }`, |
| 105 | + OperationName: json.RawMessage(`"ConsistentQuery"`), |
| 106 | + }) |
| 107 | + require.NoError(t, err) |
| 108 | + firstHash := resultContainer.Sha256Result |
| 109 | + require.NotEmpty(t, firstHash) |
| 110 | + |
| 111 | + _, err = xEnv.MakeGraphQLRequest(testenv.GraphQLRequest{ |
| 112 | + Query: `query ConsistentQuery { employees { id tag } }`, |
| 113 | + OperationName: json.RawMessage(`"ConsistentQuery"`), |
| 114 | + }) |
| 115 | + require.NoError(t, err) |
| 116 | + secondHash := resultContainer.Sha256Result |
| 117 | + require.NotEmpty(t, secondHash) |
| 118 | + |
| 119 | + require.NotEqual(t, firstHash, secondHash) |
| 120 | + }) |
| 121 | + }) |
| 122 | + |
| 123 | + t.Run("verify the same query produces same Sha256Hash", func(t *testing.T) { |
| 124 | + t.Parallel() |
| 125 | + |
| 126 | + resultContainer := &sha256_verifier.ResultContainer{} |
| 127 | + |
| 128 | + cfg := config.Config{ |
| 129 | + Graph: config.Graph{}, |
| 130 | + Modules: map[string]interface{}{ |
| 131 | + "sha256VerifierModule": sha256_verifier.Sha256VerifierModule{ |
| 132 | + ForceSha256: true, |
| 133 | + ResultContainer: resultContainer, |
| 134 | + }, |
| 135 | + }, |
| 136 | + } |
| 137 | + |
| 138 | + testenv.Run(t, &testenv.Config{ |
| 139 | + RouterOptions: []core.Option{ |
| 140 | + core.WithModulesConfig(cfg.Modules), |
| 141 | + core.WithCustomModules(&sha256_verifier.Sha256VerifierModule{}), |
| 142 | + }, |
| 143 | + }, func(t *testing.T, xEnv *testenv.Environment) { |
| 144 | + _, err := xEnv.MakeGraphQLRequest(testenv.GraphQLRequest{ |
| 145 | + Query: `query ConsistentQuery { employees { id } }`, |
| 146 | + OperationName: json.RawMessage(`"ConsistentQuery"`), |
| 147 | + }) |
| 148 | + require.NoError(t, err) |
| 149 | + firstHash := resultContainer.Sha256Result |
| 150 | + require.NotEmpty(t, firstHash) |
| 151 | + |
| 152 | + _, err = xEnv.MakeGraphQLRequest(testenv.GraphQLRequest{ |
| 153 | + Query: `query ConsistentQuery { employees { id } }`, |
| 154 | + OperationName: json.RawMessage(`"ConsistentQuery"`), |
| 155 | + }) |
| 156 | + require.NoError(t, err) |
| 157 | + secondHash := resultContainer.Sha256Result |
| 158 | + require.NotEmpty(t, secondHash) |
| 159 | + |
| 160 | + require.Equal(t, firstHash, secondHash, "Same query should produce the same SHA256 hash") |
| 161 | + }) |
| 162 | + }) |
| 163 | + |
| 164 | +} |
0 commit comments