Skip to content

Commit

Permalink
Merge branch 'main' into import_notifications
Browse files Browse the repository at this point in the history
  • Loading branch information
rowanseymour committed Sep 9, 2021
2 parents 76cd86c + 2baf4a0 commit 413f2bb
Show file tree
Hide file tree
Showing 19 changed files with 124 additions and 92 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
v6.5.24
----------
* Fix IVR for orgs using S3 sessions
* Ticket notifications (opened and activity)

v6.5.23
----------
* Add force param to close tickets endpoint to let us ignore errors on external ticket service when removing a ticketer
Expand Down
76 changes: 37 additions & 39 deletions core/models/contacts_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ func TestContacts(t *testing.T) {
assert.NotNil(t, bob.Fields()["joined"].QueryValue())
assert.Equal(t, 2, len(bob.URNs()))
assert.Equal(t, "tel:+16055742222?id=10001&priority=1000", bob.URNs()[0].String())
assert.Equal(t, "whatsapp:250788373373?id=20121&priority=999", bob.URNs()[1].String())
assert.Equal(t, "whatsapp:250788373373?id=30000&priority=999", bob.URNs()[1].String())
assert.Equal(t, 0, bob.Groups().Count())
assert.Equal(t, 0, bob.Tickets().Count()) // because ticketer no longer exists

Expand All @@ -97,7 +97,7 @@ func TestContacts(t *testing.T) {
bob, err = modelContacts[1].FlowContact(org)
assert.NoError(t, err)
assert.Equal(t, "tel:+16055742222?channel=74729f45-7f29-4868-9dc4-90e491e3c7d8&id=10001&priority=1000", bob.URNs()[0].String())
assert.Equal(t, "whatsapp:250788373373?id=20121&priority=999", bob.URNs()[1].String())
assert.Equal(t, "whatsapp:250788373373?id=30000&priority=999", bob.URNs()[1].String())

// add another tel urn to bob
testdata.InsertContactURN(db, testdata.Org1, testdata.Bob, urns.URN("tel:+250788373373"), 10)
Expand All @@ -107,40 +107,40 @@ func TestContacts(t *testing.T) {
assert.NoError(t, err)

// set our preferred channel again
err = modelContacts[0].UpdatePreferredURN(ctx, db, org, models.URNID(20122), channel)
err = modelContacts[0].UpdatePreferredURN(ctx, db, org, models.URNID(30001), channel)
assert.NoError(t, err)

bob, err = modelContacts[0].FlowContact(org)
assert.NoError(t, err)
assert.Equal(t, "tel:+250788373373?channel=74729f45-7f29-4868-9dc4-90e491e3c7d8&id=20122&priority=1000", bob.URNs()[0].String())
assert.Equal(t, "tel:+250788373373?channel=74729f45-7f29-4868-9dc4-90e491e3c7d8&id=30001&priority=1000", bob.URNs()[0].String())
assert.Equal(t, "tel:+16055742222?channel=74729f45-7f29-4868-9dc4-90e491e3c7d8&id=10001&priority=999", bob.URNs()[1].String())
assert.Equal(t, "whatsapp:250788373373?id=20121&priority=998", bob.URNs()[2].String())
assert.Equal(t, "whatsapp:250788373373?id=30000&priority=998", bob.URNs()[2].String())

// no op this time
err = modelContacts[0].UpdatePreferredURN(ctx, db, org, models.URNID(20122), channel)
err = modelContacts[0].UpdatePreferredURN(ctx, db, org, models.URNID(30001), channel)
assert.NoError(t, err)

bob, err = modelContacts[0].FlowContact(org)
assert.NoError(t, err)
assert.Equal(t, "tel:+250788373373?channel=74729f45-7f29-4868-9dc4-90e491e3c7d8&id=20122&priority=1000", bob.URNs()[0].String())
assert.Equal(t, "tel:+250788373373?channel=74729f45-7f29-4868-9dc4-90e491e3c7d8&id=30001&priority=1000", bob.URNs()[0].String())
assert.Equal(t, "tel:+16055742222?channel=74729f45-7f29-4868-9dc4-90e491e3c7d8&id=10001&priority=999", bob.URNs()[1].String())
assert.Equal(t, "whatsapp:250788373373?id=20121&priority=998", bob.URNs()[2].String())
assert.Equal(t, "whatsapp:250788373373?id=30000&priority=998", bob.URNs()[2].String())

// calling with no channel is a noop on the channel
err = modelContacts[0].UpdatePreferredURN(ctx, db, org, models.URNID(20122), nil)
err = modelContacts[0].UpdatePreferredURN(ctx, db, org, models.URNID(30001), nil)
assert.NoError(t, err)

bob, err = modelContacts[0].FlowContact(org)
assert.NoError(t, err)
assert.Equal(t, "tel:+250788373373?channel=74729f45-7f29-4868-9dc4-90e491e3c7d8&id=20122&priority=1000", bob.URNs()[0].String())
assert.Equal(t, "tel:+250788373373?channel=74729f45-7f29-4868-9dc4-90e491e3c7d8&id=30001&priority=1000", bob.URNs()[0].String())
assert.Equal(t, "tel:+16055742222?channel=74729f45-7f29-4868-9dc4-90e491e3c7d8&id=10001&priority=999", bob.URNs()[1].String())
assert.Equal(t, "whatsapp:250788373373?id=20121&priority=998", bob.URNs()[2].String())
assert.Equal(t, "whatsapp:250788373373?id=30000&priority=998", bob.URNs()[2].String())
}

func TestCreateContact(t *testing.T) {
ctx, _, db, _ := testsuite.Get()

defer testsuite.Reset()
defer testdata.ResetContactData(db)

testdata.InsertContactGroup(db, testdata.Org1, "d636c966-79c1-4417-9f1c-82ad629773a2", "Kinyarwanda", "language = kin")

Expand All @@ -155,11 +155,11 @@ func TestCreateContact(t *testing.T) {

assert.Equal(t, "Rich", contact.Name())
assert.Equal(t, envs.Language(`kin`), contact.Language())
assert.Equal(t, []urns.URN{"telegram:200001?id=20122&priority=1000", "telegram:200002?id=20121&priority=999"}, contact.URNs())
assert.Equal(t, []urns.URN{"telegram:200001?id=30001&priority=1000", "telegram:200002?id=30000&priority=999"}, contact.URNs())

assert.Equal(t, "Rich", flowContact.Name())
assert.Equal(t, envs.Language(`kin`), flowContact.Language())
assert.Equal(t, []urns.URN{"telegram:200001?id=20122&priority=1000", "telegram:200002?id=20121&priority=999"}, flowContact.URNs().RawURNs())
assert.Equal(t, []urns.URN{"telegram:200001?id=30001&priority=1000", "telegram:200002?id=30000&priority=999"}, flowContact.URNs().RawURNs())
assert.Len(t, flowContact.Groups().All(), 1)
assert.Equal(t, assets.GroupUUID("d636c966-79c1-4417-9f1c-82ad629773a2"), flowContact.Groups().All()[0].UUID())

Expand All @@ -170,7 +170,7 @@ func TestCreateContact(t *testing.T) {
func TestCreateContactRace(t *testing.T) {
ctx, _, db, _ := testsuite.Get()

defer testsuite.Reset()
defer testdata.ResetContactData(db)

oa, err := models.GetOrgAssets(ctx, db, testdata.Org1.ID)
assert.NoError(t, err)
Expand Down Expand Up @@ -199,18 +199,17 @@ func TestCreateContactRace(t *testing.T) {
func TestGetOrCreateContact(t *testing.T) {
ctx, _, db, _ := testsuite.Get()

defer testsuite.Reset()
defer testdata.ResetContactData(db)

testdata.InsertContactGroup(db, testdata.Org1, "d636c966-79c1-4417-9f1c-82ad629773a2", "Telegrammer", `telegram = 100001`)
testdata.InsertContactGroup(db, testdata.Org1, "dcc16d85-8274-4d19-a3c2-152d4ee99380", "Telegrammer", `telegram = 100001`)

// add some orphaned URNs
testdata.InsertContactURN(db, testdata.Org1, nil, urns.URN("telegram:200001"), 100)
testdata.InsertContactURN(db, testdata.Org1, nil, urns.URN("telegram:200002"), 100)

var maxContactID models.ContactID
db.Get(&maxContactID, `SELECT max(id) FROM contacts_contact`)
newContact := func() models.ContactID { maxContactID++; return maxContactID }
prevContact := func() models.ContactID { return maxContactID }
contactIDSeq := models.ContactID(30000)
newContact := func() models.ContactID { id := contactIDSeq; contactIDSeq++; return id }
prevContact := func() models.ContactID { return contactIDSeq - 1 }

oa, err := models.GetOrgAssets(ctx, db, testdata.Org1.ID)
require.NoError(t, err)
Expand Down Expand Up @@ -247,52 +246,52 @@ func TestGetOrCreateContact(t *testing.T) {
[]urns.URN{urns.URN("telegram:100001")},
newContact(), // creates new contact
true,
[]urns.URN{"telegram:100001?channel=74729f45-7f29-4868-9dc4-90e491e3c7d8&id=20123&priority=1000"},
[]urns.URN{"telegram:100001?channel=74729f45-7f29-4868-9dc4-90e491e3c7d8&id=30002&priority=1000"},
testdata.TwilioChannel.ID,
[]assets.GroupUUID{"d636c966-79c1-4417-9f1c-82ad629773a2"},
[]assets.GroupUUID{"dcc16d85-8274-4d19-a3c2-152d4ee99380"},
},
{
testdata.Org1.ID,
[]urns.URN{urns.URN("telegram:100001")},
prevContact(), // returns the same created contact
false,
[]urns.URN{"telegram:100001?channel=74729f45-7f29-4868-9dc4-90e491e3c7d8&id=20123&priority=1000"},
[]urns.URN{"telegram:100001?channel=74729f45-7f29-4868-9dc4-90e491e3c7d8&id=30002&priority=1000"},
models.NilChannelID,
[]assets.GroupUUID{"d636c966-79c1-4417-9f1c-82ad629773a2"},
[]assets.GroupUUID{"dcc16d85-8274-4d19-a3c2-152d4ee99380"},
},
{
testdata.Org1.ID,
[]urns.URN{urns.URN("telegram:100001"), urns.URN("telegram:100002")},
prevContact(), // same again as other URNs don't exist
false,
[]urns.URN{"telegram:100001?channel=74729f45-7f29-4868-9dc4-90e491e3c7d8&id=20123&priority=1000"},
[]urns.URN{"telegram:100001?channel=74729f45-7f29-4868-9dc4-90e491e3c7d8&id=30002&priority=1000"},
models.NilChannelID,
[]assets.GroupUUID{"d636c966-79c1-4417-9f1c-82ad629773a2"},
[]assets.GroupUUID{"dcc16d85-8274-4d19-a3c2-152d4ee99380"},
},
{
testdata.Org1.ID,
[]urns.URN{urns.URN("telegram:100002"), urns.URN("telegram:100001")},
prevContact(), // same again as other URNs don't exist
false,
[]urns.URN{"telegram:100001?channel=74729f45-7f29-4868-9dc4-90e491e3c7d8&id=20123&priority=1000"},
[]urns.URN{"telegram:100001?channel=74729f45-7f29-4868-9dc4-90e491e3c7d8&id=30002&priority=1000"},
models.NilChannelID,
[]assets.GroupUUID{"d636c966-79c1-4417-9f1c-82ad629773a2"},
[]assets.GroupUUID{"dcc16d85-8274-4d19-a3c2-152d4ee99380"},
},
{
testdata.Org1.ID,
[]urns.URN{urns.URN("telegram:200001"), urns.URN("telegram:100001")},
prevContact(), // same again as other URNs are orphaned
false,
[]urns.URN{"telegram:100001?channel=74729f45-7f29-4868-9dc4-90e491e3c7d8&id=20123&priority=1000"},
[]urns.URN{"telegram:100001?channel=74729f45-7f29-4868-9dc4-90e491e3c7d8&id=30002&priority=1000"},
models.NilChannelID,
[]assets.GroupUUID{"d636c966-79c1-4417-9f1c-82ad629773a2"},
[]assets.GroupUUID{"dcc16d85-8274-4d19-a3c2-152d4ee99380"},
},
{
testdata.Org1.ID,
[]urns.URN{urns.URN("telegram:100003"), urns.URN("telegram:100004")}, // 2 new URNs
newContact(),
true,
[]urns.URN{"telegram:100003?id=20124&priority=1000", "telegram:100004?id=20125&priority=999"},
[]urns.URN{"telegram:100003?id=30003&priority=1000", "telegram:100004?id=30004&priority=999"},
models.NilChannelID,
[]assets.GroupUUID{},
},
Expand All @@ -301,7 +300,7 @@ func TestGetOrCreateContact(t *testing.T) {
[]urns.URN{urns.URN("telegram:100005"), urns.URN("telegram:200002")}, // 1 new, 1 orphaned
newContact(),
true,
[]urns.URN{"telegram:100005?id=20126&priority=1000", "telegram:200002?id=20122&priority=999"},
[]urns.URN{"telegram:100005?id=30005&priority=1000", "telegram:200002?id=30001&priority=999"},
models.NilChannelID,
[]assets.GroupUUID{},
},
Expand All @@ -327,7 +326,7 @@ func TestGetOrCreateContact(t *testing.T) {
func TestGetOrCreateContactRace(t *testing.T) {
ctx, _, db, _ := testsuite.Get()

defer testsuite.Reset()
defer testdata.ResetContactData(db)

oa, err := models.GetOrgAssets(ctx, db, testdata.Org1.ID)
assert.NoError(t, err)
Expand Down Expand Up @@ -356,15 +355,14 @@ func TestGetOrCreateContactRace(t *testing.T) {
func TestGetOrCreateContactIDsFromURNs(t *testing.T) {
ctx, _, db, _ := testsuite.Get()

defer testsuite.Reset()
defer testdata.ResetContactData(db)

// add an orphaned URN
testdata.InsertContactURN(db, testdata.Org1, nil, urns.URN("telegram:200001"), 100)

var maxContactID models.ContactID
db.Get(&maxContactID, `SELECT max(id) FROM contacts_contact`)
newContact := func() models.ContactID { maxContactID++; return maxContactID }
prevContact := func() models.ContactID { return maxContactID }
contactIDSeq := models.ContactID(30000)
newContact := func() models.ContactID { id := contactIDSeq; contactIDSeq++; return id }
prevContact := func() models.ContactID { return contactIDSeq - 1 }

org, err := models.GetOrgAssets(ctx, db, testdata.Org1.ID)
assert.NoError(t, err)
Expand Down
11 changes: 8 additions & 3 deletions core/models/imports_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,12 @@ func TestContactImports(t *testing.T) {

defer testsuite.Reset()

testdata.DeleteContactsAndURNs(db)
// start with no contacts or URNs
db.MustExec(`DELETE FROM contacts_contacturn`)
db.MustExec(`DELETE FROM contacts_contactgroup_contacts`)
db.MustExec(`DELETE FROM contacts_contact`)
db.MustExec(`ALTER SEQUENCE contacts_contact_id_seq RESTART WITH 10000`)
db.MustExec(`ALTER SEQUENCE contacts_contacturn_id_seq RESTART WITH 10000`)

// add contact in other org to make sure we can't update it
testdata.InsertContact(db, testdata.Org2, "f7a8016d-69a6-434b-aae7-5142ce4a98ba", "Xavier", "spa")
Expand Down Expand Up @@ -63,7 +68,7 @@ func TestContactImports(t *testing.T) {
defer uuids.SetGenerator(uuids.DefaultGenerator)

for i, tc := range tcs {
importID := testdata.InsertContactImport(db, testdata.Org1)
importID := testdata.InsertContactImport(db, testdata.Org1, testdata.Admin)
batchID := testdata.InsertContactImportBatch(db, importID, tc.Specs)

batch, err := models.LoadContactImportBatch(ctx, db, batchID)
Expand Down Expand Up @@ -144,7 +149,7 @@ func TestContactImports(t *testing.T) {
func TestContactImportBatch(t *testing.T) {
ctx, _, db, _ := testsuite.Get()

importID := testdata.InsertContactImport(db, testdata.Org1)
importID := testdata.InsertContactImport(db, testdata.Org1, testdata.Admin)
batchID := testdata.InsertContactImportBatch(db, importID, []byte(`[
{"name": "Norbert", "language": "eng", "urns": ["tel:+16055740001"]},
{"name": "Leah", "urns": ["tel:+16055740002"]}
Expand Down
11 changes: 2 additions & 9 deletions core/tasks/contacts/import_contact_batch_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package contacts_test
import (
"fmt"
"testing"
"time"

_ "github.com/nyaruka/mailroom/core/handlers"
"github.com/nyaruka/mailroom/core/tasks/contacts"
Expand All @@ -18,13 +17,7 @@ func TestImportContactBatch(t *testing.T) {
rc := rp.Get()
defer rc.Close()

start := time.Now()

defer func() {
db.MustExec(`DELETE FROM contacts_contactgroup_contacts WHERE contact_id > $1`, testdata.Org2Contact.ID)
db.MustExec(`DELETE FROM contacts_contacturn WHERE id > $1`, testdata.Org2Contact.URNID)
db.MustExec(`DELETE FROM contacts_contact WHERE id > $1`, testdata.Org2Contact.ID)
}()
defer testdata.ResetContactData(db)

importID := testdata.InsertContactImport(db, testdata.Org1, testdata.Admin)
batch1ID := testdata.InsertContactImportBatch(db, importID, []byte(`[
Expand All @@ -50,7 +43,7 @@ func TestImportContactBatch(t *testing.T) {
err = task2.Perform(ctx, rt, testdata.Org1.ID)
require.NoError(t, err)

testsuite.AssertQuery(t, db, `SELECT count(*) FROM contacts_contact WHERE created_on > $1`, start).Returns(3)
testsuite.AssertQuery(t, db, `SELECT count(*) FROM contacts_contact WHERE id >= 30000`).Returns(3)
testsuite.AssertQuery(t, db, `SELECT count(*) FROM contacts_contact WHERE name = 'Norbert' AND language = 'eng'`).Returns(1)
testsuite.AssertQuery(t, db, `SELECT count(*) FROM contacts_contact WHERE name = 'Leah' AND language IS NULL`).Returns(1)
testsuite.AssertQuery(t, db, `SELECT count(*) FROM contacts_contact WHERE name = 'Rowan' AND language = 'spa'`).Returns(1)
Expand Down
2 changes: 1 addition & 1 deletion mailroom.go
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,7 @@ func (mr *Mailroom) Start() error {
mr.handlerForeman.Start()

// start our web server
mr.webserver = web.NewServer(mr.ctx, c, mr.rt.DB, mr.rt.RP, mr.rt.MediaStorage, mr.rt.ES, mr.wg)
mr.webserver = web.NewServer(mr.ctx, c, mr.rt.DB, mr.rt.RP, mr.rt.MediaStorage, mr.rt.SessionStorage, mr.rt.ES, mr.wg)
mr.webserver.Start()

logrus.WithField("domain", c.Domain).Info("mailroom started")
Expand Down
Binary file modified mailroom_test.dump
Binary file not shown.
30 changes: 21 additions & 9 deletions testsuite/testdata/contacts.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,14 +83,26 @@ func InsertContactURN(db *sqlx.DB, org *Org, contact *Contact, urn urns.URN, pri
return id
}

// DeleteContactsAndURNs deletes all contacts and URNs
func DeleteContactsAndURNs(db *sqlx.DB) {
// ResetContactData removes contact data not in the test database dump. Note that this function can't
// undo changes made to the contact data in the test database dump.
func ResetContactData(db *sqlx.DB) {
db.MustExec(`DELETE FROM tickets_ticketevent`)
db.MustExec(`DELETE FROM tickets_ticket`)
db.MustExec(`DELETE FROM channels_channelcount`)
db.MustExec(`DELETE FROM msgs_msg`)
db.MustExec(`DELETE FROM contacts_contacturn`)
db.MustExec(`DELETE FROM contacts_contactgroup_contacts`)
db.MustExec(`DELETE FROM contacts_contact`)

// reset id sequences back to a known number
db.MustExec(`ALTER SEQUENCE contacts_contact_id_seq RESTART WITH 10000`)
db.MustExec(`ALTER SEQUENCE contacts_contacturn_id_seq RESTART WITH 10000`)
db.MustExec(`DELETE FROM contacts_contacturn WHERE id >= 30000`)
db.MustExec(`DELETE FROM contacts_contactgroup_contacts WHERE contact_id >= 30000`)
db.MustExec(`DELETE FROM contacts_contact WHERE id >= 30000`)
db.MustExec(`DELETE FROM contacts_contactgroupcount WHERE group_id >= 30000`)
db.MustExec(`DELETE FROM contacts_contactgroup WHERE id >= 30000`)

// reset id sequences back
db.MustExec(`ALTER SEQUENCE tickets_ticket_id_seq RESTART WITH 1`)
db.MustExec(`ALTER SEQUENCE msgs_msg_id_seq RESTART WITH 1`)
db.MustExec(`ALTER SEQUENCE contacts_contact_id_seq RESTART WITH 30000`)
db.MustExec(`ALTER SEQUENCE contacts_contacturn_id_seq RESTART WITH 30000`)
db.MustExec(`ALTER SEQUENCE contacts_contactgroup_id_seq RESTART WITH 30000`)

// because groups have changed
models.FlushCache()
}
2 changes: 1 addition & 1 deletion web/contact/search_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ func TestSearch(t *testing.T) {
)
assert.NoError(t, err)

server := web.NewServer(ctx, config.Mailroom, db, rp, nil, client, wg)
server := web.NewServer(ctx, config.Mailroom, db, rp, nil, nil, client, wg)
server.Start()

// give our server time to start
Expand Down
2 changes: 1 addition & 1 deletion web/contact/testdata/create.json
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@
"timezone": "America/Los_Angeles",
"created_on": "2018-07-06T12:30:00.123457Z",
"urns": [
"tel:+16055700001?id=20121&priority=1000"
"tel:+16055700001?id=30000&priority=1000"
],
"groups": [
{
Expand Down
Loading

0 comments on commit 413f2bb

Please sign in to comment.