github.com/jancarloviray/community@v0.41.1-0.20170124221257-33a66c87cf2f/sdk/exttest/attachment.go (about) 1 // Copyright 2016 Documize Inc. <legal@documize.com>. All rights reserved. 2 // 3 // This software (Documize Community Edition) is licensed under 4 // GNU AGPL v3 http://www.gnu.org/licenses/agpl-3.0.en.html 5 // 6 // You can operate outside the AGPL restrictions by purchasing 7 // Documize Enterprise Edition and obtaining a commercial license 8 // by contacting <sales@documize.com>. 9 // 10 // https://documize.com 11 12 package exttest 13 14 import ( 15 "testing" 16 17 "github.com/documize/community/sdk" 18 ) 19 20 func testDocAttachments(t *testing.T, c *documize.Client, testFile, testData string) { 21 atts, err := c.GetAttachments(testData) 22 if err != nil { 23 t.Error(err) 24 } 25 for a := range atts { 26 err = c.GetAttachmentData(&atts[a]) 27 if err != nil { 28 t.Error(err) 29 } 30 if atts[a].Filename == "test.txt" { 31 if string(atts[a].Data) != "This is a test text file.\n" { 32 t.Error("incorrect content to attachment") 33 } 34 goto foundAtt 35 } 36 //t.Logf("DEBUG %d atts= %#v ; err=%v; data=%s", a, atts[a], err,string(atts[a].Data)) 37 } 38 t.Error("Attachment test.txt not found") 39 foundAtt: 40 dingbat := "dingbat\n" 41 err = c.AddAttachment(testFile, "dingbat.txt", []byte(dingbat)) 42 if err != nil { 43 t.Error(err) 44 } else { 45 atts, err = c.GetAttachments(testFile) 46 if err != nil { 47 t.Error(err) 48 } else { 49 if len(atts) != 1 { 50 t.Error("should be exactly 1 attachment") 51 } else { 52 err = c.GetAttachmentData(&atts[0]) 53 if err != nil { 54 t.Error(err) 55 } else { 56 if string(atts[0].Data) != dingbat { 57 t.Error("Wrong data in attachement") 58 } 59 err = c.DeleteAttachment(&atts[0]) 60 if err != nil { 61 t.Error(err) 62 } 63 atts, err = c.GetAttachments(testFile) 64 if err != nil { 65 t.Error(err) 66 } else { 67 if len(atts) != 0 { 68 t.Error("should be no attachments") 69 } 70 } 71 } 72 } 73 } 74 } 75 // errors 76 atts, err = c.GetAttachments("XXXXXXX") 77 if len(atts) != 0 { 78 if err == nil { 79 t.Error("Get attachments of unknown file did not error") 80 } else { 81 t.Log("INFO: get attachments of unknown file msg:", err) 82 } 83 } 84 /* TODO improve failure modes 85 att := &entity.Attachment{} 86 err = c.GetAttachmentData(att) 87 if len(att.Data) > 0 { 88 if err == nil { 89 t.Error("Get attachment data of blank file did not error") 90 } else { 91 t.Log("INFO: get attachments of blank file msg:", err) 92 } 93 } 94 err = c.AddAttachment("YYYYYYYYYYYY", "dingbat.txt", []byte(dingbat)) 95 if err != nil { 96 t.Error("Did not error adding attachment to bad file id") 97 } else { 98 t.Log("INFO: add attachment to unknown file msg:", err) 99 } 100 err = c.DeleteAttachment(&entity.Attachment{}) 101 if err != nil { 102 t.Error("Did not error deleting attachment of blank data") 103 } else { 104 t.Log("INFO: delete attachment to blank file msg:", err) 105 } 106 */ 107 }