github.com/cozy/cozy-stack@v0.0.0-20240603063001-31110fa4cae1/model/contact/contact_test.go (about) 1 package contact 2 3 import ( 4 "encoding/json" 5 "fmt" 6 "testing" 7 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/pkg/prefixer" 12 "github.com/gofrs/uuid/v5" 13 "github.com/stretchr/testify/assert" 14 "github.com/stretchr/testify/require" 15 ) 16 17 func TestGetAllContacts(t *testing.T) { 18 config.UseTestFile(t) 19 instPrefix := prefixer.NewPrefixer(0, "cozy-test", "cozy-test") 20 t.Cleanup(func() { _ = couchdb.DeleteDB(instPrefix, consts.Contacts) }) 21 22 g := NewGroup() 23 g.SetID(uuid.Must(uuid.NewV7()).String()) 24 25 gaby := fmt.Sprintf(`{ 26 "address": [], 27 "birthday": "", 28 "birthplace": "", 29 "company": "", 30 "cozy": [], 31 "cozyMetadata": { 32 "createdAt": "2024-02-13T15:05:58.917Z", 33 "createdByApp": "Contacts", 34 "createdByAppVersion": "1.7.0", 35 "doctypeVersion": 3, 36 "metadataVersion": 1, 37 "updatedAt": "2024-02-13T15:06:21.046Z", 38 "updatedByApps": [ 39 { 40 "date": "2024-02-13T15:06:21.046Z", 41 "slug": "Contacts", 42 "version": "1.7.0" 43 } 44 ] 45 }, 46 "displayName": "Gaby", 47 "email": [], 48 "fullname": "Gaby", 49 "gender": "female", 50 "indexes": { 51 "byFamilyNameGivenNameEmailCozyUrl": "gaby" 52 }, 53 "jobTitle": "", 54 "metadata": { 55 "cozy": true, 56 "version": 1 57 }, 58 "name": { 59 "givenName": "Gaby" 60 }, 61 "note": "", 62 "phone": [], 63 "relationships": { 64 "groups": { 65 "data": [ 66 { 67 "_id": "%s", 68 "_type": "io.cozy.contacts.groups" 69 } 70 ] 71 } 72 } 73 }`, g.ID()) 74 75 doc := couchdb.JSONDoc{Type: consts.Contacts, M: make(map[string]interface{})} 76 require.NoError(t, json.Unmarshal([]byte(gaby), &doc.M)) 77 require.NoError(t, couchdb.CreateDoc(instPrefix, &doc)) 78 79 contacts, err := g.GetAllContacts(instPrefix) 80 require.NoError(t, err) 81 require.Len(t, contacts, 1) 82 assert.Equal(t, contacts[0].PrimaryName(), "Gaby") 83 }