|
| 1 | +// Licensed to The Moov Authors under one or more contributor |
| 2 | +// license agreements. See the NOTICE file distributed with |
| 3 | +// this work for additional information regarding copyright |
| 4 | +// ownership. The Moov Authors licenses this file to you under |
| 5 | +// the Apache License, Version 2.0 (the "License"); you may |
| 6 | +// not use this file except in compliance with the License. |
| 7 | +// You may obtain a copy of the License at |
| 8 | +// |
| 9 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +// |
| 11 | +// Unless required by applicable law or agreed to in writing, |
| 12 | +// software distributed under the License is distributed on an |
| 13 | +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 14 | +// KIND, either express or implied. See the License for the |
| 15 | +// specific language governing permissions and limitations |
| 16 | +// under the License. |
| 17 | + |
| 18 | +package test |
| 19 | + |
| 20 | +import ( |
| 21 | + "context" |
| 22 | + "encoding/json" |
| 23 | + "fmt" |
| 24 | + "net/http" |
| 25 | + "net/http/httptest" |
| 26 | + "os" |
| 27 | + "path/filepath" |
| 28 | + "slices" |
| 29 | + "testing" |
| 30 | + "time" |
| 31 | + |
| 32 | + "github.com/moov-io/achgateway/internal/files" |
| 33 | + "github.com/moov-io/achgateway/internal/incoming/stream/streamtest" |
| 34 | + "github.com/moov-io/achgateway/internal/incoming/web" |
| 35 | + "github.com/moov-io/achgateway/internal/pipeline" |
| 36 | + "github.com/moov-io/achgateway/internal/service" |
| 37 | + "github.com/moov-io/achgateway/internal/shards" |
| 38 | + "github.com/moov-io/achgateway/internal/storage" |
| 39 | + "github.com/moov-io/achgateway/pkg/models" |
| 40 | + "github.com/moov-io/base" |
| 41 | + "github.com/moov-io/base/database" |
| 42 | + "github.com/moov-io/base/log" |
| 43 | + |
| 44 | + "github.com/gorilla/mux" |
| 45 | + "github.com/stretchr/testify/require" |
| 46 | +) |
| 47 | + |
| 48 | +func TestCancelFileAPI(t *testing.T) { |
| 49 | + ctx := context.Background() |
| 50 | + logger := log.NewTestLogger() |
| 51 | + |
| 52 | + dir := t.TempDir() |
| 53 | + conf := &service.Config{ |
| 54 | + Sharding: service.Sharding{ |
| 55 | + Shards: []service.Shard{ |
| 56 | + { |
| 57 | + Name: "testing", |
| 58 | + Cutoffs: service.Cutoffs{ |
| 59 | + Timezone: "America/New_York", |
| 60 | + Windows: []string{"12:00"}, |
| 61 | + }, |
| 62 | + UploadAgent: "mock", |
| 63 | + }, |
| 64 | + }, |
| 65 | + Default: "testing", |
| 66 | + }, |
| 67 | + Upload: service.UploadAgents{ |
| 68 | + Agents: []service.UploadAgent{ |
| 69 | + { |
| 70 | + Mock: &service.MockAgent{}, |
| 71 | + }, |
| 72 | + }, |
| 73 | + Merging: service.Merging{ |
| 74 | + Storage: storage.Config{ |
| 75 | + Filesystem: storage.FilesystemConfig{ |
| 76 | + Directory: dir, |
| 77 | + }, |
| 78 | + }, |
| 79 | + }, |
| 80 | + }, |
| 81 | + } |
| 82 | + |
| 83 | + httpFilesTopic, httpFilesSub := streamtest.InmemStream(t) |
| 84 | + |
| 85 | + shardRepo := shards.NewInMemoryRepository() |
| 86 | + shardRepo.Add(service.ShardMapping{ShardKey: "testing", ShardName: "testing"}, database.NopInTx) |
| 87 | + |
| 88 | + fileRepo := &files.MockRepository{} |
| 89 | + |
| 90 | + fileReceiver, err := pipeline.Start(ctx, logger, conf, shardRepo, fileRepo, httpFilesSub) |
| 91 | + require.NoError(t, err) |
| 92 | + |
| 93 | + controller := web.NewFilesController(logger, service.HTTPConfig{}, httpFilesTopic, fileReceiver.CancellationResponses) |
| 94 | + r := mux.NewRouter() |
| 95 | + controller.AppendRoutes(r) |
| 96 | + |
| 97 | + // Accept file from stream |
| 98 | + fileID := base.ID() |
| 99 | + |
| 100 | + fd, err := os.Open(filepath.Join("..", "..", "testdata", "ppd-debit.ach")) |
| 101 | + require.NoError(t, err) |
| 102 | + t.Cleanup(func() { fd.Close() }) |
| 103 | + |
| 104 | + req := httptest.NewRequest("POST", fmt.Sprintf("/shards/SD-testing/files/%s.ach", fileID), fd) |
| 105 | + w := httptest.NewRecorder() |
| 106 | + r.ServeHTTP(w, req) |
| 107 | + require.Equal(t, http.StatusOK, w.Code) |
| 108 | + |
| 109 | + // Verify the file is written |
| 110 | + where := filepath.Join(dir, "mergable", "testing", "*.ach") |
| 111 | + require.Eventually(t, func() bool { |
| 112 | + filenames, err := filepath.Glob(where) |
| 113 | + require.NoError(t, err) |
| 114 | + |
| 115 | + parent, _ := filepath.Split(where) |
| 116 | + return slices.Contains(filenames, filepath.Join(parent, fmt.Sprintf("%s.ach", fileID))) |
| 117 | + }, 10*time.Second, 1*time.Second) |
| 118 | + |
| 119 | + // Now cancel that file |
| 120 | + req = httptest.NewRequest("DELETE", fmt.Sprintf("/shards/SD-testing/files/%s.ach", fileID), nil) |
| 121 | + w = httptest.NewRecorder() |
| 122 | + r.ServeHTTP(w, req) |
| 123 | + require.Equal(t, http.StatusOK, w.Code) |
| 124 | + |
| 125 | + var response models.FileCancellationResponse |
| 126 | + err = json.NewDecoder(w.Body).Decode(&response) |
| 127 | + require.NoError(t, err) |
| 128 | + require.Equal(t, fileID, response.FileID) |
| 129 | + require.Equal(t, "SD-testing", response.ShardKey) |
| 130 | + require.True(t, response.Successful) |
| 131 | + |
| 132 | + // Cancel it again (which should be successful) |
| 133 | + req = httptest.NewRequest("DELETE", fmt.Sprintf("/shards/SD-testing/files/%s.ach", fileID), nil) |
| 134 | + w = httptest.NewRecorder() |
| 135 | + r.ServeHTTP(w, req) |
| 136 | + require.Equal(t, http.StatusOK, w.Code) |
| 137 | + |
| 138 | + var response2 models.FileCancellationResponse |
| 139 | + err = json.NewDecoder(w.Body).Decode(&response2) |
| 140 | + require.NoError(t, err) |
| 141 | + require.Equal(t, fileID, response2.FileID) |
| 142 | + require.Equal(t, "SD-testing", response2.ShardKey) |
| 143 | + require.True(t, response2.Successful) |
| 144 | +} |
0 commit comments