forked from rapidpro/mailroom
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
sow for searching API, add unit test
- Loading branch information
1 parent
dbf277d
commit d6dfab6
Showing
7 changed files
with
183 additions
and
41 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,123 @@ | ||
package contact | ||
|
||
import ( | ||
"bytes" | ||
"encoding/json" | ||
"fmt" | ||
"io" | ||
"io/ioutil" | ||
"net/http" | ||
"strings" | ||
"sync" | ||
"testing" | ||
"time" | ||
|
||
"github.com/nyaruka/mailroom/config" | ||
"github.com/nyaruka/mailroom/models" | ||
"github.com/nyaruka/mailroom/search" | ||
"github.com/nyaruka/mailroom/testsuite" | ||
"github.com/nyaruka/mailroom/web" | ||
"github.com/olivere/elastic" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestServer(t *testing.T) { | ||
testsuite.Reset() | ||
ctx := testsuite.CTX() | ||
db := testsuite.DB() | ||
rp := testsuite.RP() | ||
wg := &sync.WaitGroup{} | ||
|
||
es := search.NewMockElasticServer() | ||
defer es.Close() | ||
|
||
client, err := elastic.NewClient( | ||
elastic.SetURL(es.URL()), | ||
elastic.SetHealthcheck(false), | ||
elastic.SetSniff(false), | ||
) | ||
assert.NoError(t, err) | ||
|
||
server := web.NewServer(ctx, config.Mailroom, db, rp, nil, client, wg) | ||
server.Start() | ||
|
||
// give our server time to start | ||
time.Sleep(time.Second) | ||
|
||
defer server.Stop() | ||
|
||
tcs := []struct { | ||
URL string | ||
Method string | ||
Body string | ||
Status int | ||
Response string | ||
Hits []models.ContactID | ||
ESResponse string | ||
}{ | ||
{"/mr/contact/search", "GET", "", 405, "illegal", nil, ""}, | ||
{ | ||
"/mr/contact/search", "POST", | ||
fmt.Sprintf(`{"org_id": 1, "query": "Cathy", "group_uuid": "%s"}`, models.AllContactsGroupUUID), | ||
200, | ||
`name~Cathy`, | ||
[]models.ContactID{models.CathyID}, | ||
fmt.Sprintf(`{ | ||
"_scroll_id": "DXF1ZXJ5QW5kRmV0Y2gBAAAAAAAbgc0WS1hqbHlfb01SM2lLTWJRMnVOSVZDdw==", | ||
"took": 2, | ||
"timed_out": false, | ||
"_shards": { | ||
"total": 1, | ||
"successful": 1, | ||
"skipped": 0, | ||
"failed": 0 | ||
}, | ||
"hits": { | ||
"total": 1, | ||
"max_score": null, | ||
"hits": [ | ||
{ | ||
"_index": "contacts", | ||
"_type": "_doc", | ||
"_id": "%d", | ||
"_score": null, | ||
"_routing": "1", | ||
"sort": [ | ||
15124352 | ||
] | ||
} | ||
] | ||
} | ||
}`, models.CathyID), | ||
}, | ||
} | ||
|
||
for i, tc := range tcs { | ||
var body io.Reader | ||
es.NextResponse = tc.ESResponse | ||
|
||
if tc.Body != "" { | ||
body = bytes.NewReader([]byte(tc.Body)) | ||
} | ||
|
||
req, err := http.NewRequest(tc.Method, "http://localhost:8090"+tc.URL, body) | ||
assert.NoError(t, err, "%d: error creating request", i) | ||
|
||
resp, err := http.DefaultClient.Do(req) | ||
assert.NoError(t, err, "%d: error making request", i) | ||
|
||
assert.Equal(t, tc.Status, resp.StatusCode, "%d: unexpected status", i) | ||
|
||
content, err := ioutil.ReadAll(resp.Body) | ||
assert.NoError(t, err, "%d: error reading body", i) | ||
assert.True(t, strings.Contains(string(content), tc.Response), "%d: did not find string: %s in body: %s", i, tc.Response, string(content)) | ||
|
||
// on 200 responses parse them | ||
if resp.StatusCode == 200 { | ||
r := &searchResponse{} | ||
err = json.Unmarshal(content, r) | ||
assert.NoError(t, err) | ||
assert.Equal(t, tc.Hits, r.ContactIDs) | ||
} | ||
} | ||
} |