github.com/cozy/cozy-stack@v0.0.0-20240603063001-31110fa4cae1/model/job/trigger_share_group_test.go (about)

     1  package job
     2  
     3  import (
     4  	"encoding/json"
     5  	"testing"
     6  
     7  	"github.com/cozy/cozy-stack/pkg/consts"
     8  	"github.com/cozy/cozy-stack/pkg/couchdb"
     9  	"github.com/cozy/cozy-stack/pkg/realtime"
    10  	"github.com/stretchr/testify/assert"
    11  	"github.com/stretchr/testify/require"
    12  )
    13  
    14  func TestShareGroupTrigger(t *testing.T) {
    15  	trigger := &ShareGroupTrigger{}
    16  
    17  	t.Run("The contact becomes invitable", func(t *testing.T) {
    18  		justName := &couchdb.JSONDoc{
    19  			Type: consts.Contacts,
    20  			M: map[string]interface{}{
    21  				"_id":      "85507320-b157-013c-12d8-18c04daba325",
    22  				"_rev":     "1-abcdef",
    23  				"fullname": "Bob",
    24  			},
    25  		}
    26  		msg := trigger.match(&realtime.Event{
    27  			Doc:    justName,
    28  			OldDoc: nil,
    29  			Verb:   realtime.EventCreate,
    30  		})
    31  		require.Nil(t, msg)
    32  
    33  		withAnEmail := justName.Clone().(*couchdb.JSONDoc)
    34  		withAnEmail.M["email"] = []interface{}{
    35  			map[string]interface{}{
    36  				"address": "bob@example.net",
    37  			},
    38  		}
    39  		withAnEmail.M["_rev"] = "2-abcdef"
    40  		msg = trigger.match(&realtime.Event{
    41  			Doc:    withAnEmail,
    42  			OldDoc: justName,
    43  			Verb:   realtime.EventUpdate,
    44  		})
    45  		require.NotNil(t, msg)
    46  		assert.Equal(t, msg.ContactID, "85507320-b157-013c-12d8-18c04daba325")
    47  		assert.Len(t, msg.GroupsAdded, 0)
    48  		assert.Len(t, msg.GroupsRemoved, 0)
    49  		assert.True(t, msg.BecomeInvitable)
    50  
    51  		withCozyURL := justName.Clone().(*couchdb.JSONDoc)
    52  		withCozyURL.M["cozy"] = []interface{}{
    53  			map[string]interface{}{
    54  				"url": "bob.mycozy.cloud",
    55  			},
    56  		}
    57  		withCozyURL.M["_rev"] = "2-abcdef"
    58  		msg = trigger.match(&realtime.Event{
    59  			Doc:    withCozyURL,
    60  			OldDoc: justName,
    61  			Verb:   realtime.EventUpdate,
    62  		})
    63  		require.NotNil(t, msg)
    64  		assert.Equal(t, msg.ContactID, "85507320-b157-013c-12d8-18c04daba325")
    65  		assert.Len(t, msg.GroupsAdded, 0)
    66  		assert.Len(t, msg.GroupsRemoved, 0)
    67  		assert.True(t, msg.BecomeInvitable)
    68  
    69  		both := withAnEmail.Clone().(*couchdb.JSONDoc)
    70  		both.M["cozy"] = []interface{}{
    71  			map[string]interface{}{
    72  				"url": "bob.mycozy.cloud",
    73  			},
    74  		}
    75  		both.M["_rev"] = "3-abcdef"
    76  		msg = trigger.match(&realtime.Event{
    77  			Doc:    both,
    78  			OldDoc: withAnEmail,
    79  			Verb:   realtime.EventUpdate,
    80  		})
    81  		assert.Nil(t, msg)
    82  	})
    83  
    84  	t.Run("Groups are added/removed to a contact", func(t *testing.T) {
    85  		noGroup := &couchdb.JSONDoc{
    86  			Type: consts.Contacts,
    87  			M: map[string]interface{}{
    88  				"_id":      "85507320-b157-013c-12d8-18c04daba326",
    89  				"_rev":     "1-abcdef",
    90  				"fullname": "Bob",
    91  			},
    92  		}
    93  		msg := trigger.match(&realtime.Event{
    94  			Doc:    noGroup,
    95  			OldDoc: nil,
    96  			Verb:   realtime.EventCreate,
    97  		})
    98  		require.Nil(t, msg)
    99  
   100  		updatedName := noGroup.Clone().(*couchdb.JSONDoc)
   101  		updatedName.M["fullname"] = "Bobby"
   102  		updatedName.M["_rev"] = "2-abcdef"
   103  		msg = trigger.match(&realtime.Event{
   104  			Doc:    updatedName,
   105  			OldDoc: noGroup,
   106  			Verb:   realtime.EventUpdate,
   107  		})
   108  		require.Nil(t, msg)
   109  
   110  		var groups map[string]interface{}
   111  		require.NoError(t, json.Unmarshal([]byte(`{
   112      "groups": {
   113        "data": [
   114          {
   115            "_id": "id-friends",
   116            "_type": "io.cozy.contacts.groups"
   117          },
   118          {
   119            "_id": "id-football",
   120            "_type": "io.cozy.contacts.groups"
   121          }
   122        ]
   123      }
   124  }`), &groups))
   125  		addedInGroups := updatedName.Clone().(*couchdb.JSONDoc)
   126  		addedInGroups.M["relationships"] = groups
   127  		addedInGroups.M["_rev"] = "3-abcdef"
   128  		msg = trigger.match(&realtime.Event{
   129  			Doc:    addedInGroups,
   130  			OldDoc: updatedName,
   131  			Verb:   realtime.EventUpdate,
   132  		})
   133  		require.NotNil(t, msg)
   134  		assert.Equal(t, msg.ContactID, "85507320-b157-013c-12d8-18c04daba326")
   135  		assert.EqualValues(t, msg.GroupsAdded, []string{"id-friends", "id-football"})
   136  		assert.Len(t, msg.GroupsRemoved, 0)
   137  		assert.False(t, msg.BecomeInvitable)
   138  
   139  		var groups2 map[string]interface{}
   140  		require.NoError(t, json.Unmarshal([]byte(`{
   141      "groups": {
   142        "data": [
   143          {
   144            "_id": "id-friends",
   145            "_type": "io.cozy.contacts.groups"
   146          }
   147        ]
   148      }
   149  }`), &groups2))
   150  		removedFromFootball := addedInGroups.Clone().(*couchdb.JSONDoc)
   151  		removedFromFootball.M["relationships"] = groups2
   152  		removedFromFootball.M["_rev"] = "4-abcdef"
   153  		msg = trigger.match(&realtime.Event{
   154  			Doc:    removedFromFootball,
   155  			OldDoc: addedInGroups,
   156  			Verb:   realtime.EventUpdate,
   157  		})
   158  		require.NotNil(t, msg)
   159  		assert.Equal(t, msg.ContactID, "85507320-b157-013c-12d8-18c04daba326")
   160  		assert.Len(t, msg.GroupsAdded, 0)
   161  		assert.EqualValues(t, msg.GroupsRemoved, []string{"id-football"})
   162  		assert.False(t, msg.BecomeInvitable)
   163  
   164  		deleted := &couchdb.JSONDoc{
   165  			Type: consts.Contacts,
   166  			M: map[string]interface{}{
   167  				"_id":      removedFromFootball.ID(),
   168  				"_rev":     "5-abcdef",
   169  				"_deleted": true,
   170  			},
   171  		}
   172  		msg = trigger.match(&realtime.Event{
   173  			Doc:    deleted,
   174  			OldDoc: removedFromFootball,
   175  			Verb:   realtime.EventDelete,
   176  		})
   177  		require.NotNil(t, msg)
   178  		assert.Equal(t, msg.ContactID, "85507320-b157-013c-12d8-18c04daba326")
   179  		assert.Len(t, msg.GroupsAdded, 0)
   180  		assert.EqualValues(t, msg.GroupsRemoved, []string{"id-friends"})
   181  		assert.False(t, msg.BecomeInvitable)
   182  	})
   183  }