github.com/cozy/cozy-stack@v0.0.0-20240603063001-31110fa4cae1/web/contacts/contacts_test.go (about)

     1  package contacts
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/cozy/cozy-stack/model/contact"
     7  	"github.com/cozy/cozy-stack/model/instance/lifecycle"
     8  	"github.com/cozy/cozy-stack/pkg/config/config"
     9  	"github.com/cozy/cozy-stack/pkg/consts"
    10  	"github.com/cozy/cozy-stack/pkg/couchdb"
    11  	"github.com/cozy/cozy-stack/tests/testutils"
    12  	"github.com/gavv/httpexpect/v2"
    13  	"github.com/stretchr/testify/assert"
    14  )
    15  
    16  func TestContacts(t *testing.T) {
    17  	if testing.Short() {
    18  		t.Skip("an instance is required for this test: test skipped due to the use of --short flag")
    19  	}
    20  
    21  	config.UseTestFile(t)
    22  	testutils.NeedCouchdb(t)
    23  	setup := testutils.NewSetup(t, t.Name())
    24  	testInstance := setup.GetTestInstance(&lifecycle.Options{
    25  		Email:      "alice@example.com",
    26  		PublicName: "Alice",
    27  	})
    28  	_, token := setup.GetTestClient(consts.Contacts)
    29  	ts := setup.GetTestServer("/contacts", Routes)
    30  	t.Cleanup(ts.Close)
    31  
    32  	t.Run("Myself", func(t *testing.T) {
    33  		e := testutils.CreateTestClient(t, ts.URL)
    34  
    35  		// Create an empty contact for myself
    36  		e.POST("/contacts/myself").
    37  			WithHeader("Authorization", "Bearer "+token).
    38  			Expect().Status(200)
    39  
    40  		// Get the contact info about myself
    41  		myself, err := contact.GetMyself(testInstance)
    42  		assert.NoError(t, err)
    43  
    44  		// Delete the contacts info about myself
    45  		err = couchdb.DeleteDoc(testInstance, myself)
    46  		assert.NoError(t, err)
    47  
    48  		// Create again an empty contact for myself ?
    49  		obj := e.POST("/contacts/myself").
    50  			WithHeader("Authorization", "Bearer "+token).
    51  			Expect().Status(200).
    52  			JSON(httpexpect.ContentOpts{MediaType: "application/vnd.api+json"}).
    53  			Object()
    54  
    55  		// Check the contact data
    56  		data := obj.Value("data").Object()
    57  		data.Value("id").String().NotEmpty()
    58  		data.ValueEqual("type", consts.Contacts)
    59  
    60  		meta := data.Value("meta").Object()
    61  		meta.Value("rev").String().NotEmpty()
    62  
    63  		attrs := data.Value("attributes").Object()
    64  		attrs.ValueEqual("fullname", "Alice")
    65  		emails := attrs.Value("email").Array()
    66  		emails.Length().Equal(1)
    67  		email := emails.First().Object()
    68  		email.ValueEqual("address", "alice@example.com")
    69  		email.ValueEqual("primary", true)
    70  	})
    71  }