Repository: moov-io/customers
Pull request #191 (2020-09-24 15:28:13)

templater 3: move documents + disclamers and storage (documents) into own packages
extract documents, disclaimers + storage


Patch:
diff --git a/cmd/server/main.go b/cmd/server/main.go
index 5d2e917..5745c7e 100644
--- a/cmd/server/main.go
+++ b/cmd/server/main.go
@@ -25,6 +25,8 @@ import (
 	"github.com/moov-io/customers/pkg/accounts"
 	"github.com/moov-io/customers/pkg/configuration"
 	"github.com/moov-io/customers/pkg/customers"
+	"github.com/moov-io/customers/pkg/documents"
+	"github.com/moov-io/customers/pkg/documents/storage"
 	"github.com/moov-io/customers/pkg/fed"
 	"github.com/moov-io/customers/pkg/paygate"
 	"github.com/moov-io/customers/pkg/secrets"
@@ -90,8 +92,8 @@ func main() {
 	accountsRepo := accounts.NewRepo(logger, db)
 	customerRepo := customers.NewCustomerRepo(logger, db)
 	customerSSNRepo := customers.NewCustomerSSNRepository(logger, db)
-	disclaimerRepo := customers.NewDisclaimerRepo(logger, db)
-	documentRepo := customers.NewDocumentRepo(logger, db)
+	disclaimerRepo := documents.NewDisclaimerRepo(logger, db)
+	documentRepo := documents.NewDocumentRepo(logger, db)
 	validationsRepo := validator.NewRepo(db)
 
 	// Start Admin server (with Prometheus metrics)
@@ -130,7 +132,7 @@ func main() {
 
 	// Register our admin routes
 	customers.AddApprovalRoutes(logger, adminServer, customerRepo, customerSSNRepo, ofac)
-	customers.AddDisclaimerAdminRoutes(logger, adminServer, disclaimerRepo, documentRepo)
+	documents.AddDisclaimerAdminRoutes(logger, adminServer, disclaimerRepo, documentRepo)
 
 	// Setup Customer SSN storage wrapper
 	ctx := context.Background()
@@ -168,8 +170,8 @@ func main() {
 	addPingRoute(router)
 	accounts.RegisterRoutes(logger, router, accountsRepo, validationsRepo, fedClient, stringKeeper, transitStringKeeper, validationStrategies, &accountOfacSeacher)
 	customers.AddCustomerRoutes(logger, router, customerRepo, customerSSNStorage, ofac)
-	customers.AddDisclaimerRoutes(logger, router, disclaimerRepo)
-	customers.AddDocumentRoutes(logger, router, documentRepo, customers.GetBucket(bucketName, cloudProvider, signer))
+	documents.AddDisclaimerRoutes(logger, router, disclaimerRepo)
+	documents.AddDocumentRoutes(logger, router, documentRepo, storage.GetBucket(bucketName, cloudProvider, signer))
 	customers.AddOFACRoutes(logger, router, customerRepo, ofac)
 
 	// Add Configuration routes
@@ -179,7 +181,7 @@ func main() {
 	// Optionally serve /files/ as our fileblob routes
 	// Note: FILEBLOB_BASE_URL needs to match something that's routed to /files/...
 	if cloudProvider == "file" {
-		customers.AddFileblobRoutes(logger, router, signer, customers.GetBucket(bucketName, cloudProvider, signer))
+		storage.AddFileblobRoutes(logger, router, signer, storage.GetBucket(bucketName, cloudProvider, signer))
 	}
 
 	// Start business HTTP server
@@ -246,7 +248,7 @@ func setupStorageBucket(logger log.Logger, bucketName, cloudProvider string) *fi
 			secret = "secret"
 			logger.Log("main", "WARNING!!!! USING INSECURE DEFAULT FILE STORAGE, set FILEBLOB_HMAC_SECRET for ANY production usage")
 		}
-		signer, err := customers.FileblobSigner(baseURL, secret)
+		signer, err := storage.FileblobSigner(baseURL, secret)
 		if err != nil {
 			panic(fmt.Sprintf("fileBucket: %v", err))
 		}
diff --git a/pkg/customers/disclaimers.go b/pkg/documents/disclaimers.go
similarity index 99%
rename from pkg/customers/disclaimers.go
rename to pkg/documents/disclaimers.go
index 6698367..3b660c8 100644
--- a/pkg/customers/disclaimers.go
+++ b/pkg/documents/disclaimers.go
@@ -2,7 +2,7 @@
 // Use of this source code is governed by an Apache License
 // license that can be found in the LICENSE file.
 
-package customers
+package documents
 
 import (
 	"database/sql"
diff --git a/pkg/customers/documents.go b/pkg/documents/documents.go
similarity index 96%
rename from pkg/customers/documents.go
rename to pkg/documents/documents.go
index 0221204..8b407e1 100644
--- a/pkg/customers/documents.go
+++ b/pkg/documents/documents.go
@@ -2,7 +2,7 @@
 // Use of this source code is governed by an Apache License
 // license that can be found in the LICENSE file.
 
-package customers
+package documents
 
 import (
 	"bytes"
@@ -19,6 +19,7 @@ import (
 	"github.com/moov-io/base"
 	moovhttp "github.com/moov-io/base/http"
 	"github.com/moov-io/customers/pkg/client"
+	"github.com/moov-io/customers/pkg/documents/storage"
 	"github.com/moov-io/customers/pkg/route"
 
 	"github.com/go-kit/kit/log"
@@ -32,7 +33,7 @@ var (
 	maxDocumentSize int64 = 20 * 1024 * 1024 // 20MB
 )
 
-func AddDocumentRoutes(logger log.Logger, r *mux.Router, repo DocumentRepository, bucketFactory bucketFunc) {
+func AddDocumentRoutes(logger log.Logger, r *mux.Router, repo DocumentRepository, bucketFactory storage.BucketFunc) {
 	r.Methods("GET").Path("/customers/{customerID}/documents").HandlerFunc(getCustomerDocuments(logger, repo))
 	r.Methods("POST").Path("/customers/{customerID}/documents").HandlerFunc(uploadCustomerDocument(logger, repo, bucketFactory))
 	r.Methods("GET").Path("/customers/{customerID}/documents/{documentId}").HandlerFunc(retrieveRawDocument(logger, repo, bucketFactory))
@@ -80,7 +81,7 @@ func readDocumentType(v string) (string, error) {
 	return "", fmt.Errorf("unknown Document type: %s", orig)
 }
 
-func uploadCustomerDocument(logger log.Logger, repo DocumentRepository, bucketFactory bucketFunc) http.HandlerFunc {
+func uploadCustomerDocument(logger log.Logger, repo DocumentRepository, bucketFactory storage.BucketFunc) http.HandlerFunc {
 	return func(w http.ResponseWriter, r *http.Request) {
 		w = route.Responder(logger, w, r)
 		// TODO(adam): should we store x-namespace along with the Document?
@@ -161,7 +162,7 @@ func uploadCustomerDocument(logger log.Logger, repo DocumentRepository, bucketFa
 	}
 }
 
-func retrieveRawDocument(logger log.Logger, repo DocumentRepository, bucketFactory bucketFunc) http.HandlerFunc {
+func retrieveRawDocument(logger log.Logger, repo DocumentRepository, bucketFactory storage.BucketFunc) http.HandlerFunc {
 	return func(w http.ResponseWriter, r *http.Request) {
 		w = route.Responder(logger, w, r)
 
diff --git a/pkg/customers/bucket.go b/pkg/documents/storage/bucket.go
similarity index 96%
rename from pkg/customers/bucket.go
rename to pkg/documents/storage/bucket.go
index a4fa6e4..90d6006 100644
--- a/pkg/customers/bucket.go
+++ b/pkg/documents/storage/bucket.go
@@ -2,7 +2,7 @@
 // Use of this source code is governed by an Apache License
 // license that can be found in the LICENSE file.
 
-package customers
+package storage
 
 import (
 	"context"
@@ -22,9 +22,9 @@ import (
 	"gocloud.dev/gcp"
 )
 
-type bucketFunc func() (*blob.Bucket, error)
+type BucketFunc func() (*blob.Bucket, error)
 
-func GetBucket(bucketName, cloudProvider string, FileblobSigner *fileblob.URLSignerHMAC) bucketFunc {
+func GetBucket(bucketName, cloudProvider string, FileblobSigner *fileblob.URLSignerHMAC) BucketFunc {
 	return func() (*blob.Bucket, error) {
 		ctx, cancelFn := context.WithTimeout(context.TODO(), 10*time.Second)
 		defer cancelFn()
diff --git a/pkg/customers/files.go b/pkg/documents/storage/files.go
similarity index 93%
rename from pkg/customers/files.go
rename to pkg/documents/storage/files.go
index 3fca817..40a5042 100644
--- a/pkg/customers/files.go
+++ b/pkg/documents/storage/files.go
@@ -2,7 +2,7 @@
 // Use of this source code is governed by an Apache License
 // license that can be found in the LICENSE file.
 
-package customers
+package storage
 
 import (
 	"context"
@@ -21,7 +21,7 @@ import (
 	"gocloud.dev/blob/fileblob"
 )
 
-func AddFileblobRoutes(logger log.Logger, r *mux.Router, signer *fileblob.URLSignerHMAC, bucketFactory bucketFunc) {
+func AddFileblobRoutes(logger log.Logger, r *mux.Router, signer *fileblob.URLSignerHMAC, bucketFactory BucketFunc) {
 	if v := os.Getenv("FILEBLOB_BASE_URL"); v != "" {
 		u, err := url.Parse(v)
 		if u != nil && err == nil {
@@ -32,7 +32,7 @@ func AddFileblobRoutes(logger log.Logger, r *mux.Router, signer *fileblob.URLSig
 	}
 }
 
-func proxyLocalFile(logger log.Logger, signer *fileblob.URLSignerHMAC, bucketFactory bucketFunc) http.HandlerFunc {
+func proxyLocalFile(logger log.Logger, signer *fileblob.URLSignerHMAC, bucketFactory BucketFunc) http.HandlerFunc {
 	return func(w http.ResponseWriter, r *http.Request) {
 		w = route.Responder(logger, w, r)
 


Test patch:
diff --git a/pkg/customers/disclaimers_test.go b/pkg/documents/disclaimers_test.go
similarity index 99%
rename from pkg/customers/disclaimers_test.go
rename to pkg/documents/disclaimers_test.go
index d81ca71..9dfad0f 100644
--- a/pkg/customers/disclaimers_test.go
+++ b/pkg/documents/disclaimers_test.go
@@ -2,7 +2,7 @@
 // Use of this source code is governed by an Apache License
 // license that can be found in the LICENSE file.
 
-package customers
+package documents
 
 import (
 	"bytes"
diff --git a/pkg/customers/documents_test.go b/pkg/documents/documents_test.go
similarity index 94%
rename from pkg/customers/documents_test.go
rename to pkg/documents/documents_test.go
index 9b9d65c..9579e07 100644
--- a/pkg/customers/documents_test.go
+++ b/pkg/documents/documents_test.go
@@ -2,7 +2,7 @@
 // Use of this source code is governed by an Apache License
 // license that can be found in the LICENSE file.
 
-package customers
+package documents
 
 import (
 	"bytes"
@@ -22,6 +22,7 @@ import (
 	"github.com/moov-io/base"
 	"github.com/moov-io/customers/internal/database"
 	"github.com/moov-io/customers/pkg/client"
+	"github.com/moov-io/customers/pkg/documents/storage"
 
 	"github.com/go-kit/kit/log"
 	"github.com/gorilla/mux"
@@ -65,7 +66,7 @@ func TestDocuments__getCustomerDocuments(t *testing.T) {
 	req.Header.Set("x-request-id", "test")
 
 	router := mux.NewRouter()
-	AddDocumentRoutes(log.NewNopLogger(), router, repo, testBucket)
+	AddDocumentRoutes(log.NewNopLogger(), router, repo, storage.TestBucket)
 	router.ServeHTTP(w, req)
 	w.Flush()
 
@@ -112,7 +113,7 @@ func TestDocuments__readDocumentType(t *testing.T) {
 }
 
 func multipartRequest(t *testing.T) *http.Request {
-	fd, err := os.Open(filepath.Join("..", "..", "testdata", "colorado.jpg"))
+	fd, err := os.Open(filepath.Join("testdata", "colorado.jpg"))
 	if err != nil {
 		t.Fatal(err)
 	}
@@ -148,7 +149,7 @@ func TestDocumentsUploadAndRetrieval(t *testing.T) {
 	req.Header.Set("x-request-id", "test")
 
 	router := mux.NewRouter()
-	AddDocumentRoutes(log.NewNopLogger(), router, repo, testBucket)
+	AddDocumentRoutes(log.NewNopLogger(), router, repo, storage.TestBucket)
 	router.ServeHTTP(w, req)
 	w.Flush()
 
@@ -195,7 +196,7 @@ func TestDocuments__uploadCustomerDocument(t *testing.T) {
 	req.URL = u // replace query params with invalid values
 
 	router := mux.NewRouter()
-	AddDocumentRoutes(log.NewNopLogger(), router, repo, testBucket)
+	AddDocumentRoutes(log.NewNopLogger(), router, repo, storage.TestBucket)
 	router.ServeHTTP(w, req)
 	w.Flush()
 
diff --git a/pkg/customers/bucket_test.go b/pkg/documents/storage/bucket_test.go
similarity index 76%
rename from pkg/customers/bucket_test.go
rename to pkg/documents/storage/bucket_test.go
index 990b2ab..414b126 100644
--- a/pkg/customers/bucket_test.go
+++ b/pkg/documents/storage/bucket_test.go
@@ -2,31 +2,12 @@
 // Use of this source code is governed by an Apache License
 // license that can be found in the LICENSE file.
 
-package customers
+package storage
 
 import (
 	"context"
-	"fmt"
 	"io/ioutil"
 	"testing"
-	"time"
-
-	"gocloud.dev/blob"
-)
-
-var (
-	testBucket bucketFunc = func() (*blob.Bucket, error) {
-		signer, err := FileblobSigner("http://localhost:8087", "secret")
-		if err != nil {
-			panic(fmt.Sprintf("testBucket: %v", err))
-		}
-
-		ctx, cancelFn := context.WithTimeout(context.TODO(), 1*time.Second)
-		defer cancelFn()
-
-		dir, _ := ioutil.TempDir("", "testBucket")
-		return fileBucket(ctx, dir, signer)
-	}
 )
 
 func TestBucket__openBucket(t *testing.T) {
diff --git a/pkg/customers/files_test.go b/pkg/documents/storage/files_test.go
similarity index 88%
rename from pkg/customers/files_test.go
rename to pkg/documents/storage/files_test.go
index 4936c99..3ec349c 100644
--- a/pkg/customers/files_test.go
+++ b/pkg/documents/storage/files_test.go
@@ -2,7 +2,7 @@
 // Use of this source code is governed by an Apache License
 // license that can be found in the LICENSE file.
 
-package customers
+package storage
 
 import (
 	"context"
@@ -15,8 +15,6 @@ import (
 	"testing"
 	"time"
 
-	"github.com/moov-io/base"
-
 	"github.com/go-kit/kit/log"
 	"github.com/gorilla/mux"
 	"gocloud.dev/blob"
@@ -24,13 +22,13 @@ import (
 
 func TestFiles__proxyLocalFile(t *testing.T) {
 	// first, upload a file
-	bucket, err := testBucket()
+	bucket, err := TestBucket()
 	if err != nil {
 		t.Fatal(err)
 	}
 
-	customerID, documentID := base.ID(), base.ID()
-	documentKey := makeDocumentKey(customerID, documentID)
+	// customerID, documentID := base.ID(), base.ID()
+	documentKey := "hello" // makeDocumentKey(customerID, documentID)
 
 	ctx, cancelFn := context.WithTimeout(context.TODO(), 10*time.Second)
 	defer cancelFn()
@@ -42,7 +40,7 @@ func TestFiles__proxyLocalFile(t *testing.T) {
 	if err != nil {
 		t.Fatal(err)
 	}
-	fd, err := os.Open(filepath.Join("..", "..", "testdata", "colorado.jpg"))
+	fd, err := os.Open(filepath.Join("..", "testdata", "colorado.jpg"))
 	if err != nil {
 		t.Fatal(err)
 	}
diff --git a/pkg/documents/storage/test_bucket.go b/pkg/documents/storage/test_bucket.go
new file mode 100644
index 0000000..8d5ca3b
--- /dev/null
+++ b/pkg/documents/storage/test_bucket.go
@@ -0,0 +1,25 @@
+package storage
+
+import (
+	"context"
+	"fmt"
+	"io/ioutil"
+	"time"
+
+	"gocloud.dev/blob"
+)
+
+var (
+	TestBucket BucketFunc = func() (*blob.Bucket, error) {
+		signer, err := FileblobSigner("http://localhost:8087", "secret")
+		if err != nil {
+			panic(fmt.Sprintf("testBucket: %v", err))
+		}
+
+		ctx, cancelFn := context.WithTimeout(context.TODO(), 1*time.Second)
+		defer cancelFn()
+
+		dir, _ := ioutil.TempDir("", "testBucket")
+		return fileBucket(ctx, dir, signer)
+	}
+)