Skip to content

Commit

Permalink
test(v1/documents) Implements tests for DocumentContent
Browse files Browse the repository at this point in the history
Signed-off-by: Pierre PÉRONNET <[email protected]>
  • Loading branch information
holyhope committed Apr 25, 2024
1 parent 63ee7fb commit 26cf1da
Showing 1 changed file with 65 additions and 6 deletions.
71 changes: 65 additions & 6 deletions v1/document_test.go
Original file line number Diff line number Diff line change
@@ -1,25 +1,66 @@
package digiposte_test

import (
"fmt"
"mime"
"strings"

"github.com/onsi/ginkgo/v2"
"github.com/onsi/gomega"
"github.com/onsi/gomega/gbytes"

"github.com/holyhope/digiposte-go-sdk/v1"
)

var _ = ginkgo.Describe("Document", func() {
var document *digiposte.Document

ginkgo.AfterEach(func(ctx ginkgo.SpecContext) {
if document == nil {
return
}

if err := digiposteClient.Trash(ctx, []digiposte.DocumentID{document.InternalID}, nil); err != nil {
fmt.Fprintf(ginkgo.GinkgoWriter, "trash: %v\n", err)
}

if err := digiposteClient.Delete(ctx, []digiposte.DocumentID{document.InternalID}, nil); err != nil {
fmt.Fprintf(ginkgo.GinkgoWriter, "delete: %v\n", err)
}
})

ginkgo.Describe("CreateDocument", func() {
var document *digiposte.Document
ginkgo.Context("When the document does not exist", func() {
ginkgo.It("Should create a document", func(ctx ginkgo.SpecContext) {
var err error

document, err = digiposteClient.CreateDocument(ctx,
digiposte.RootFolderID,
ginkgo.CurrentSpecReport().FullText(),
strings.NewReader("the content"),
digiposte.DocumentTypeBasic,
)
gomega.Expect(err).ToNot(gomega.HaveOccurred())
gomega.Expect(document.InternalID).ToNot(gomega.BeEmpty())
})
})
})

ginkgo.AfterEach(func(ctx ginkgo.SpecContext) {
gomega.Expect(digiposteClient.Trash(ctx, []digiposte.DocumentID{document.InternalID}, nil)).To(gomega.Succeed())
gomega.Expect(digiposteClient.Delete(ctx, []digiposte.DocumentID{document.InternalID}, nil)).To(gomega.Succeed())
ginkgo.Describe("DocumentContent", func() {
ginkgo.Context("When the document does not exist", func() {
ginkgo.BeforeEach(func() {
document = new(digiposte.Document)
document.InternalID = "i-do-not-exist"
})

ginkgo.It("Should return an error", func(ctx ginkgo.SpecContext) {
_, _, err := digiposteClient.DocumentContent(ctx, document.InternalID)
gomega.Expect(err).To(gomega.HaveOccurred())
})
})

ginkgo.Context("when the document does not exist", func() {
ginkgo.It("should create a document", func(ctx ginkgo.SpecContext) {
ginkgo.Context("When the document exist", func() {
ginkgo.BeforeEach(func(ctx ginkgo.SpecContext) {
var err error

document, err = digiposteClient.CreateDocument(ctx,
Expand All @@ -31,6 +72,24 @@ var _ = ginkgo.Describe("Document", func() {
gomega.Expect(err).ToNot(gomega.HaveOccurred())
gomega.Expect(document.InternalID).ToNot(gomega.BeEmpty())
})

ginkgo.AfterEach(func(ctx ginkgo.SpecContext) {
gomega.Expect(digiposteClient.Trash(ctx, []digiposte.DocumentID{document.InternalID}, nil)).To(gomega.Succeed())
gomega.Expect(digiposteClient.Delete(ctx, []digiposte.DocumentID{document.InternalID}, nil)).To(gomega.Succeed())
})

ginkgo.It("Should return the content and the right Content-Type", func(ctx ginkgo.SpecContext) {
content, contentType, err := digiposteClient.DocumentContent(ctx, document.InternalID)
gomega.Expect(err).ToNot(gomega.HaveOccurred())
gomega.Eventually(gbytes.BufferReader(content)).Should(gbytes.Say("^the content$"))

mediaType, params, err := mime.ParseMediaType(contentType)
gomega.Expect(err).ToNot(gomega.HaveOccurred())
gomega.Expect(mediaType).To(gomega.Equal("text/plain"))
gomega.Expect(params).To(gomega.HaveKeyWithValue(
"charset", gomega.WithTransform(strings.ToLower, gomega.Equal("utf-8")),
))
})
})
})
})

0 comments on commit 26cf1da

Please sign in to comment.