Skip to content

Commit

Permalink
Merge pull request #27 from weni-ai/increase-coverage
Browse files Browse the repository at this point in the history
Increase coverage
  • Loading branch information
rasoro authored Jul 7, 2023
2 parents 68a0754 + d436ffb commit 0d38225
Show file tree
Hide file tree
Showing 13 changed files with 1,024 additions and 76 deletions.
42 changes: 42 additions & 0 deletions core/models/channel_connection_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package models_test

import (
"testing"
"time"

"github.com/nyaruka/mailroom/core/models"
"github.com/nyaruka/mailroom/testsuite"
Expand All @@ -28,4 +29,45 @@ func TestChannelConnections(t *testing.T) {
conn2, err := models.SelectChannelConnection(ctx, db, conn.ID())
assert.NoError(t, err)
assert.Equal(t, "test1", conn2.ExternalID())
assert.Equal(t, testdata.Org1.ID, conn2.OrgID())
assert.Equal(t, models.ConnectionStatus("W"), conn2.Status())
assert.Equal(t, models.ConnectionError(""), conn2.ErrorReason())
assert.Equal(t, 0, conn2.ErrorCount())
assert.Equal(t, models.StartID(0), conn2.StartID())
var noMoment *time.Time = nil
assert.Equal(t, noMoment, conn2.NextAttempt())

conn3, err := models.SelectChannelConnectionByExternalID(ctx, db, testdata.TwilioChannel.ID, models.ConnectionTypeIVR, "test1")
assert.NoError(t, err)
assert.Equal(t, "test1", conn3.ExternalID())

connCount, err := models.ActiveChannelConnectionCount(ctx, db, testdata.TwilioChannel.ID)
assert.NoError(t, err)
assert.Equal(t, 1, connCount)

err = conn3.MarkStarted(ctx, db, time.Now())
assert.NoError(t, err)

err = conn3.MarkThrottled(ctx, db, time.Now())
assert.NoError(t, err)

err = conn3.UpdateStatus(ctx, db, models.ConnectionStatusQueued, 1, time.Now())
assert.NoError(t, err)

// next time attempt will be in 10 milliseconds
mockDuration := time.Millisecond * 10
err = conn3.MarkErrored(ctx, db, time.Now(), &mockDuration, models.ConnectionError(models.ConnectionStatusErrored))
assert.NoError(t, err)

// after 10 milliseconds test1 connection will be available to be retried
time.Sleep(100 * time.Millisecond)
conns, err := models.LoadChannelConnectionsToRetry(ctx, db, 10)
assert.NoError(t, err)
assert.Equal(t, 1, len(conns))

err = models.UpdateChannelConnectionStatuses(ctx, db, []models.ConnectionID{conn3.ID()}, models.ConnectionStatusErrored)
assert.NoError(t, err)

err = conn3.MarkFailed(ctx, db, time.Now())
assert.NoError(t, err)
}
12 changes: 6 additions & 6 deletions core/models/external_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -198,24 +198,24 @@ type PromptUUID uuids.UUID

type Prompt struct {
p struct {
ID PromptID `db:"id"`
UUID PromptUUID `db:"uuid"`
Text string `db:"org_id"`
ExternalServiceID ExternalServiceID `db:"external_service_id"`
ID PromptID `db:"id"`
UUID PromptUUID `db:"uuid"`
Text string `db:"text"`
ChatGPTServiceID ExternalServiceID `db:"chat_gpt_service_id"`
}
}

func (p *Prompt) ID() PromptID { return p.p.ID }
func (p *Prompt) UUID() PromptUUID { return p.p.UUID }
func (p *Prompt) Text() string { return p.p.Text }
func (p *Prompt) ExternalServiceID() ExternalServiceID { return p.p.ExternalServiceID }
func (p *Prompt) ExternalServiceID() ExternalServiceID { return p.p.ChatGPTServiceID }

const selectPromptsByExternalServiceIDSQL = `
SELECT
p.id as id,
p.uuid as uuid,
p."text" as "text",
p.chat_gpt_service_id as chat_gpt_service_id,
p.chat_gpt_service_id as chat_gpt_service_id
FROM
public.externals_prompt p
WHERE
Expand Down
68 changes: 67 additions & 1 deletion core/models/external_service_test.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
package models_test

import (
"context"
"testing"
"time"

"github.com/nyaruka/goflow/assets"
"github.com/nyaruka/goflow/assets/static"
"github.com/nyaruka/goflow/flows"
"github.com/nyaruka/mailroom/core/models"
_ "github.com/nyaruka/mailroom/services/external/omie"
"github.com/nyaruka/mailroom/testsuite"
"github.com/nyaruka/mailroom/testsuite/testdata"
"github.com/stretchr/testify/assert"
Expand All @@ -20,7 +25,6 @@ var esTest = &struct {

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

defer testsuite.Reset(testsuite.ResetAll)

es, err := models.LookupExternalServiceByUUID(ctx, db, esTest.UUID)
Expand All @@ -30,6 +34,8 @@ func TestExternalServices(t *testing.T) {
assert.Equal(t, "External Service Weni", es.Name())
assert.Equal(t, "1234-abcd", es.Config("service_id"))
assert.Equal(t, "543210", es.Config("service_token"))
assert.Equal(t, models.OrgID(1), es.OrgID())
assert.Equal(t, "omie", es.Type())

org1, err := models.GetOrgAssets(ctx, rt, testdata.Org1.ID)
assert.NoError(t, err)
Expand All @@ -51,4 +57,64 @@ func TestExternalServices(t *testing.T) {

assert.Equal(t, "foo", es.Config("new_key"))
assert.Equal(t, "", es.Config("service_id"))

reference := es.Reference()
assert.Equal(t, "External Service Weni", reference.Name)
assert.Equal(t, esTest.UUID, reference.UUID)

_, err = es.AsService(rt.Config, flows.NewExternalService(static.NewExternalService(es.UUID(), es.Name(), es.Type())))
assert.NoError(t, err)

_, err = db.Exec(promptDDL)
if err != nil {
t.Fatal(err)
}

_, err = db.Exec(`INSERT INTO public.externals_externalservice
(is_active, created_on, modified_on, uuid, external_service_type, "name", config, created_by_id, modified_by_id, org_id)
VALUES(true, '2023-06-16 19:14:03.690', '2023-06-16 19:14:03.690', '0f7f704f-b50a-4bf6-8903-9ae34df14ebd'::uuid, 'chatgpt', 'chatgpt_test', '{"rules": "rule test", "top_p": "0.1", "api_key": "sk-123", "ai_model": "gpt-3.5-turbo", "temperature": "0.1", "knowledge_base": "knowledge base test"}'::jsonb, $1, $2, $3);`,
testdata.Admin.ID, testdata.Admin.ID, testdata.Org1.ID)
if err != nil {
t.Fatal(err)
}

_, err = db.Exec(`INSERT INTO public.externals_prompt
(is_active, created_on, modified_on, uuid, "text", chat_gpt_service_id, created_by_id, modified_by_id)
VALUES(true, '2023-06-19 18:01:31.357', '2023-06-19 18:01:31.357', '1ca7a60b-3408-47c7-a232-8f7a84140572', 'prompt test', $1, $2, $3);`,
2, testdata.Admin.ID, testdata.Org1.ID)
if err != nil {
t.Fatal(err)
}

ctxp, cancelp := context.WithTimeout(ctx, time.Second*5)
defer cancelp()
prompts, err := models.SelectPromptsByExternalServiceID(ctxp, db, 2)
assert.NoError(t, err)
assert.Equal(t, 1, len(prompts))
assert.Equal(t, models.PromptID(1), prompts[0].ID())
assert.Equal(t, "prompt test", prompts[0].Text())
assert.Equal(t, models.PromptUUID("1ca7a60b-3408-47c7-a232-8f7a84140572"), prompts[0].UUID())
assert.Equal(t, models.ExternalServiceID(2), prompts[0].ExternalServiceID())
}

const (
promptDDL = `
CREATE TABLE public.externals_prompt (
id serial4 NOT NULL,
is_active bool NOT NULL,
created_on timestamptz NOT NULL,
modified_on timestamptz NOT NULL,
uuid varchar(36) NOT NULL,
"text" text NOT NULL,
chat_gpt_service_id int4 NOT NULL,
created_by_id int4 NOT NULL,
modified_by_id int4 NOT NULL,
CONSTRAINT externals_prompt_pkey PRIMARY KEY (id),
CONSTRAINT externals_prompt_uuid_key UNIQUE (uuid)
);
CREATE INDEX externals_prompt_chat_gpt_service_id_b582ecb2 ON public.externals_prompt USING btree (chat_gpt_service_id);
CREATE INDEX externals_prompt_created_by_id_f24926d4 ON public.externals_prompt USING btree (created_by_id);
CREATE INDEX externals_prompt_modified_by_id_c69ce3ab ON public.externals_prompt USING btree (modified_by_id);
CREATE INDEX externals_prompt_uuid_f141c6b8_like ON public.externals_prompt USING btree (uuid varchar_pattern_ops);
`
)
95 changes: 95 additions & 0 deletions core/models/runs_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
package models_test

import (
"testing"
"time"

"github.com/nyaruka/gocommon/uuids"
"github.com/nyaruka/goflow/assets"
"github.com/nyaruka/goflow/flows"
"github.com/nyaruka/goflow/flows/engine"
"github.com/nyaruka/goflow/flows/triggers"
"github.com/nyaruka/mailroom/core/models"
"github.com/nyaruka/mailroom/core/runner"
"github.com/nyaruka/mailroom/testsuite"
"github.com/nyaruka/mailroom/testsuite/testdata"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func TestRuns(t *testing.T) {
ctx, rt, db, _ := testsuite.Get()
defer testsuite.Reset(testsuite.ResetAll)
defer uuids.SetGenerator(uuids.DefaultGenerator)
uuids.SetGenerator(uuids.NewSeededGenerator(12345))

oa, err := models.GetOrgAssetsWithRefresh(ctx, rt, testdata.Org1.ID, models.RefreshFlows)
assert.NoError(t, err)

// test session returns
session := insertTestSession(t, ctx, rt, testdata.Org1, testdata.Cathy, testdata.Favorites)
assert.Equal(t, models.SessionID(1), session.ID())
assert.Equal(t, flows.SessionUUID("1ae96956-4b34-433e-8d1a-f05fe6923d6d"), session.UUID())
assert.Equal(t, flows.ContactUUID("6393abc0-283d-4c9b-a1b3-641a035c34bf"), session.ContactUUID())
assert.Equal(t, models.FlowType("M"), session.SessionType())
assert.Equal(t, true, session.Responded())
assert.Equal(t, "", session.Output())
assert.Equal(t, models.OrgID(1), session.OrgID())
assert.Equal(t, time.Now().Day(), session.CreatedOn().Day())
assert.Nil(t, session.EndedOn())
assert.Nil(t, session.TimeoutOn())
assert.Nil(t, session.WaitStartedOn())
assert.Equal(t, models.FlowID(0), session.CurrentFlowID())
assert.Nil(t, session.ConnectionID())
assert.Equal(t, 0, len(session.Runs()))
assert.Nil(t, session.Sprint())
session.ClearTimeoutOn()
sessionScene := session.Scene()
assert.NotNil(t, sessionScene)

// test NewSession, FlowSession, WriteUpdatedSession
eng := engine.NewBuilder().Build()
flowRef := assets.NewFlowReference(testdata.SingleMessage.UUID, "Test")

_, cathy := testdata.Cathy.Load(db, oa)

trigger := triggers.NewBuilder(oa.Env(), flowRef, cathy).Manual().Build()
fs, sprint, err := eng.NewSession(oa.SessionAssets(), trigger)
require.NoError(t, err)
tx, err := rt.DB.BeginTxx(ctx, nil)
require.NoError(t, err)
sess, err := models.NewSession(ctx, tx, oa, fs, sprint)
require.NoError(t, err)
err = tx.Commit()
require.NoError(t, err)
scene := models.NewSceneForSession(sess)
tx, err = rt.DB.BeginTxx(ctx, nil)
require.NoError(t, err)
err = models.HandleEvents(ctx, rt, tx, oa, scene, sprint.Events())
require.NoError(t, err)
err = models.ApplyEventPreCommitHooks(ctx, rt, tx, oa, []*models.Scene{scene})
require.NoError(t, err)
err = tx.Commit()
require.NoError(t, err)

rt.Config.DB = "postgres://mailroom_test:temba@localhost/mailroom_test?sslmode=disable&Timezone=UTC"

contactIDs := []models.ContactID{testdata.Cathy.ID}
start := models.NewFlowStart(models.OrgID(1), models.StartTypeManual, models.FlowTypeMessaging, testdata.SingleMessage.ID, true, true).
WithContactIDs(contactIDs)
batch := start.CreateBatch(contactIDs, true, len(contactIDs))

sessions, err := runner.StartFlowBatch(ctx, rt, batch)
assert.NoError(t, err)

csession := sessions[0]

fcs, err := csession.FlowSession(rt.Config, oa.SessionAssets(), oa.Env())
assert.NoError(t, err)

tx, err = rt.DB.BeginTxx(ctx, nil)
require.NoError(t, err)
err = csession.WriteUpdatedSession(ctx, rt, tx, oa, fcs, sprint, nil)
assert.NoError(t, err)
tx.Commit()
}
35 changes: 35 additions & 0 deletions core/models/tokens_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package models_test

import (
"testing"

"github.com/nyaruka/mailroom/core/models"
"github.com/nyaruka/mailroom/testsuite"
"github.com/nyaruka/mailroom/testsuite/testdata"
"github.com/stretchr/testify/assert"
)

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

defer testsuite.Reset(testsuite.ResetAll)

_, err := models.LookupOrgByUUIDAndToken(ctx, db, "123", "Administrators", "123")
assert.EqualError(t, err, `pq: invalid input syntax for type uuid: "123"`)

adminToken := "5c26a50841ff48237238bbdd021150f6a33a4199"

orNone, err := models.LookupOrgByUUIDAndToken(ctx, db, testdata.Org1.UUID, "Administrators", adminToken)
assert.NoError(t, err)
assert.Nil(t, orNone)

db.MustExec(`INSERT INTO public.api_apitoken
(is_active, "key", created, org_id, role_id, user_id)
VALUES(true, $1, 'now()', $2, 8, 3);
`, adminToken, testdata.Org1.ID)

or, err := models.LookupOrgByUUIDAndToken(ctx, db, testdata.Org1.UUID, "Administrators", adminToken)
assert.NoError(t, err)
assert.Equal(t, testdata.Org1.ID, or.ID)
assert.Equal(t, testdata.Org1.UUID, or.UUID)
}
18 changes: 9 additions & 9 deletions services/external/omie/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ type errorResponse struct {
Faultcode string `json:"faultcode"`
}

func (c *baseClient) request(method, url string, params *url.Values, body, response interface{}) (*httpx.Trace, error) {
func (c *baseClient) Request(method, url string, params *url.Values, body, response interface{}) (*httpx.Trace, error) {
b, err := json.Marshal(body)
if err != nil {
return nil, err
Expand Down Expand Up @@ -79,8 +79,8 @@ func (c *baseClient) request(method, url string, params *url.Values, body, respo
return trace, nil
}

func (c *baseClient) post(url string, params *url.Values, payload, response interface{}) (*httpx.Trace, error) {
return c.request("POST", url, params, payload, response)
func (c *baseClient) Post(url string, params *url.Values, payload, response interface{}) (*httpx.Trace, error) {
return c.Request("POST", url, params, payload, response)
}

func (c *Client) IncluirContato(data *IncluirContatoRequest) (*IncluirContatoResponse, *httpx.Trace, error) {
Expand All @@ -91,7 +91,7 @@ func (c *Client) IncluirContato(data *IncluirContatoRequest) (*IncluirContatoRes
data.AppKey = c.appKey
data.AppSecret = c.appSecret

trace, err := c.post(requestUrl, nil, data, response)
trace, err := c.Post(requestUrl, nil, data, response)
if err != nil {
return nil, trace, err
}
Expand All @@ -106,7 +106,7 @@ func (c *Client) IncluirOportunidade(data *IncluirOportunidadeRequest) (*OpInclu
data.AppKey = c.appKey
data.AppSecret = c.appSecret

trace, err := c.post(requestUrl, nil, data, response)
trace, err := c.Post(requestUrl, nil, data, response)
if err != nil {
return nil, trace, err
}
Expand All @@ -121,7 +121,7 @@ func (c *Client) ListarClientes(data *ListarClientesRequest) (*ListarClientesRes
data.AppKey = c.appKey
data.AppSecret = c.appSecret

trace, err := c.post(requestUrl, nil, data, response)
trace, err := c.Post(requestUrl, nil, data, response)
if err != nil {
return nil, trace, err
}
Expand All @@ -136,7 +136,7 @@ func (c *Client) PesquisarLancamentos(data *PesquisarLancamentosRequest) (*Pesqu
data.AppKey = c.appKey
data.AppSecret = c.appSecret

trace, err := c.post(requestUrl, nil, data, response)
trace, err := c.Post(requestUrl, nil, data, response)
if err != nil {
return nil, trace, err
}
Expand All @@ -151,7 +151,7 @@ func (c *Client) VerificarContato(data *VerificarContatoRequest) (*VerificarCont
data.AppKey = c.appKey
data.AppSecret = c.appSecret

trace, err := c.post(requestUrl, nil, data, response)
trace, err := c.Post(requestUrl, nil, data, response)
if err != nil {
return nil, trace, err
}
Expand All @@ -166,7 +166,7 @@ func (c *Client) ObterBoleto(data *ObterBoletoRequest) (*ObterBoletoResponse, *h
data.AppKey = c.appKey
data.AppSecret = c.appSecret

trace, err := c.post(requestUrl, nil, data, response)
trace, err := c.Post(requestUrl, nil, data, response)
if err != nil {
return nil, trace, err
}
Expand Down
Loading

0 comments on commit 0d38225

Please sign in to comment.