Skip to content
This repository has been archived by the owner on Sep 19, 2021. It is now read-only.

Commit

Permalink
Merge pull request #1739 from 18F/wml-delete-attachments-on-kickback
Browse files Browse the repository at this point in the history
delete attachments on kickback
  • Loading branch information
macrael authored Jun 26, 2019
2 parents 0dd55a4 + c6bc52e commit 0e6765f
Show file tree
Hide file tree
Showing 5 changed files with 165 additions and 19 deletions.
46 changes: 37 additions & 9 deletions api/admin/reject.go
Original file line number Diff line number Diff line change
@@ -1,24 +1,25 @@
package admin

import (
"fmt"
"github.com/pkg/errors"
"strings"

"github.com/18F/e-QIP-prototype/api"
"github.com/18F/e-QIP-prototype/api/pdf"
)

// Rejecter is used to reject/kickback an application
type Rejecter struct {
db api.DatabaseService
store api.StorageService
pdf api.PdfService
}

// NewRejecter returns a configured Rejecter
func NewRejecter(db api.DatabaseService, store api.StorageService, pdf api.PdfService) Rejecter {
func NewRejecter(db api.DatabaseService, store api.StorageService) Rejecter {
return Rejecter{
db,
store,
pdf,
}
}

Expand All @@ -29,12 +30,7 @@ func (r Rejecter) Reject(account api.Account) error {
return errors.Wrap(err, "Reject failed to unlock account")
}

// TODO: port over PDF.RemovePdfs.
// err = r.PDF.RemovePdfs(account)
// if err != nil {
// return errors.Wrap(err, "Reject failed to remove PDFs")
// }

// Load the application, clear the nos, save the application.
app, loadErr := r.store.LoadApplication(account.ID)
if loadErr != nil {
if loadErr == api.ErrApplicationDoesNotExist {
Expand All @@ -53,5 +49,37 @@ func (r Rejecter) Reject(account api.Account) error {
return errors.Wrap(saveErr, "Unable to save application after rejecting it")
}

// load the pfds, remove the signature pdfs.
attachmentsMetadata, listErr := r.store.ListAttachmentsMetadata(account.ID)
if listErr != nil {
return errors.Wrap(listErr, "Failed to load the current attachments in order to delete them")
}

// Get the list of PDF Types that correspond to the signed releases
releaseTypeSet := make(map[string]bool)
for _, release := range pdf.ReleasePDFs {
releaseTypeSet[release.DocType] = true
}

var deletionErrors []error
for _, attachment := range attachmentsMetadata {
if _, ok := releaseTypeSet[attachment.DocType]; ok {
delErr := r.store.DeleteAttachment(account.ID, attachment.ID)
if delErr != nil {
deletionErrors = append(deletionErrors, delErr)
}
}
}

if len(deletionErrors) != 0 {
var errStrings []string
for _, err := range deletionErrors {
errStrings = append(errStrings, err.Error())
}
joinedErrs := strings.Join(errStrings, ", ")

return errors.New(fmt.Sprintf("Got an error deleting %d documents: [%s]", len(deletionErrors), joinedErrs))
}

return nil
}
6 changes: 3 additions & 3 deletions api/integration/clear_yes_no_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ func TestClearEmptyAccount(t *testing.T) {
services := cleanTestServices(t)
account := createTestAccount(t, services.db)

rejector := admin.NewRejecter(services.db, services.store, nil)
rejector := admin.NewRejecter(services.db, services.store)

err := rejector.Reject(account)
if err != nil {
Expand Down Expand Up @@ -107,7 +107,7 @@ func rejectSection(t *testing.T, services serviceSet, json []byte, sectionName s
t.Fatal("Failed to save JSON", resp.StatusCode)
}

rejector := admin.NewRejecter(services.db, services.store, nil)
rejector := admin.NewRejecter(services.db, services.store)
err := rejector.Reject(account)
if err != nil {
t.Fatal("Failed to reject account: ", err)
Expand Down Expand Up @@ -1036,7 +1036,7 @@ func TestClearComplexSectionNos(t *testing.T) {
t.Fatal("Failed to save JSON", resp.StatusCode)
}

rejector := admin.NewRejecter(services.db, services.store, nil)
rejector := admin.NewRejecter(services.db, services.store)
err := rejector.Reject(account)
if err != nil {
t.Fatal("Failed to reject account: ", err)
Expand Down
123 changes: 123 additions & 0 deletions api/integration/reject_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
package integration

import (
"encoding/json"
"io/ioutil"
"net/http/httptest"
"testing"

"github.com/18F/e-QIP-prototype/api"
"github.com/18F/e-QIP-prototype/api/admin"
"github.com/18F/e-QIP-prototype/api/http"
"github.com/18F/e-QIP-prototype/api/pdf"
"github.com/18F/e-QIP-prototype/api/xml"
)

func TestDeleteSingaturePDFs(t *testing.T) {

services := cleanTestServices(t)
account := createTestAccount(t, services.db)

xmlService := xml.NewXMLService("../templates/")
pdfService := pdf.NewPDFService("../pdf/templates/")
submitter := admin.NewSubmitter(services.db, services.store, xmlService, pdfService)

submitHandler := http.SubmitHandler{
Env: services.env,
Log: services.log,
Database: services.db,
Store: services.store,
Submitter: submitter,
}

// First, upload an attachment.
certificationPath := "../testdata/attachments/signature-form.pdf"

certificationBytes := readTestData(t, certificationPath)

req := postAttachmentRequest(t, "signature-form.pdf", certificationBytes, account.ID)

w := httptest.NewRecorder()

createAttachmentHandler := http.AttachmentSaveHandler{
Env: services.env,
Log: services.log,
Database: services.db,
Store: services.store,
}

createAttachmentHandler.ServeHTTP(w, req)

resp := w.Result()

if resp.StatusCode != 200 {
t.Fatal("Got an error back from CreateAttachment")
}

// Created an uploaded attachment.

// Now submit, which will generate signatures.
// Setup a test scenario
form := readTestData(t, "../testdata/complete-scenarios/test1.json")
saveFormJSON(t, services, form, account.ID)
// in addition to the base form data, we need submission data to get the date signed
submissionJSON := readTestData(t, "../testdata/submission-test1.json")
saveResp := saveJSON(services, submissionJSON, account.ID)
if saveResp.StatusCode != 200 {
t.Fatal("Didn't save the submission section")
}

// call the /form/submit handler. It's a dummy handler that just returns
// the XML on success.
w, submitReq := standardResponseAndRequest("POST", "/me/form/submit", nil, account.ID)
submitHandler.ServeHTTP(w, submitReq)

submitResp := w.Result()

if submitResp.StatusCode != 200 {
t.Fatal("submit didn't succeed")
}

// Reject this submission
rejector := admin.NewRejecter(services.db, services.store)
err := rejector.Reject(account)
if err != nil {
t.Fatal("Failed to reject account: ", err)
}

// Finally, check that only the orignally uploaded attachment remains
listAttachmentHandler := http.AttachmentListHandler{
Env: services.env,
Log: services.log,
Database: services.db,
Store: services.store,
}

w, indexReq := standardResponseAndRequest("GET", "/me/attachments/", nil, account.ID)

listAttachmentHandler.ServeHTTP(w, indexReq)

indexResp := w.Result()

if indexResp.StatusCode != 200 {
t.Log("Got an error back from ListAttachments")
t.Fail()
}

body, readErr := ioutil.ReadAll(indexResp.Body)
if readErr != nil {
t.Fatal(readErr)
}

retrievedAttachments := []api.Attachment{}

jsonErr := json.Unmarshal(body, &retrievedAttachments)
if jsonErr != nil {
t.Fatal(jsonErr)
}

if len(retrievedAttachments) != 1 {
t.Fatal("Got back too many attachments: ", len(retrievedAttachments))
}

}
7 changes: 1 addition & 6 deletions api/integration/save_attachment_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,12 +137,7 @@ func TestListNoAttachments(t *testing.T) {
Store: services.store,
}

indexReq := httptest.NewRequest("GET", "/me/attachments/", nil)

getAuthCtx := http.SetAccountIDInRequestContext(indexReq, account.ID)
indexReq = indexReq.WithContext(getAuthCtx)

w := httptest.NewRecorder()
w, indexReq := standardResponseAndRequest("GET", "/me/attachments/", nil, account.ID)

listAttachmentHandler.ServeHTTP(w, indexReq)

Expand Down
2 changes: 1 addition & 1 deletion api/integration/submission_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ func TestSubmitter(t *testing.T) {

// call the /form/submit handler. It's a dummy handler that just returns
// the XML on success.
w, req := standardResponseAndRequest("GET", "/me/form/submit", nil, account.ID)
w, req := standardResponseAndRequest("POST", "/me/form/submit", nil, account.ID)
submitHandler.ServeHTTP(w, req)

submitResp := w.Result()
Expand Down

0 comments on commit 0e6765f

Please sign in to comment.