|
| 1 | +// Copyright 2017, Google, Inc. |
| 2 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 3 | +// you may not use this file except in compliance with the License. |
| 4 | +// You may obtain a copy of the License at |
| 5 | +// |
| 6 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 7 | +// |
| 8 | +// Unless required by applicable law or agreed to in writing, software |
| 9 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 10 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 11 | +// See the License for the specific language governing permissions and |
| 12 | +// limitations under the License. |
| 13 | + |
| 14 | +// NOTE: |
| 15 | +// This app can only be fully tested when deployed, because |
| 16 | +// Pub/Sub requires a live endpoint URL to hit. Nevertheless, |
| 17 | +// these tests mock it and partially test it locally. |
| 18 | + |
| 19 | +'use strict'; |
| 20 | + |
| 21 | +const test = require(`ava`); |
| 22 | +const path = require(`path`); |
| 23 | +const utils = require(`@google-cloud/nodejs-repo-tools`); |
| 24 | + |
| 25 | +const message = `This is a test message sent at: `; |
| 26 | +const payload = message + Date.now(); |
| 27 | + |
| 28 | +const cwd = path.join(__dirname, `../`); |
| 29 | +const requestObj = utils.getRequest({ cwd: cwd }); |
| 30 | + |
| 31 | +test.serial.cb(`should send a message to Pub/Sub`, (t) => { |
| 32 | + requestObj |
| 33 | + .post(`/`) |
| 34 | + .type('form') |
| 35 | + .send({ payload: payload }) |
| 36 | + .expect(200) |
| 37 | + .expect((response) => { |
| 38 | + t.is(response.text, `Message sent`); |
| 39 | + }) |
| 40 | + .end(t.end); |
| 41 | +}); |
| 42 | + |
| 43 | +test.serial.cb(`should receive incoming Pub/Sub messages`, (t) => { |
| 44 | + requestObj |
| 45 | + .post(`/pubsub/push`) |
| 46 | + .query({ token: process.env.PUBSUB_VERIFICATION_TOKEN }) |
| 47 | + .send({ |
| 48 | + message: { |
| 49 | + data: payload |
| 50 | + } |
| 51 | + }) |
| 52 | + .expect(200) |
| 53 | + .end(t.end); |
| 54 | +}); |
| 55 | + |
| 56 | +test.serial.cb(`should check for verification token on incoming Pub/Sub messages`, (t) => { |
| 57 | + requestObj |
| 58 | + .post(`/pubsub/push`) |
| 59 | + .field(`payload`, payload) |
| 60 | + .expect(400) |
| 61 | + .end(t.end); |
| 62 | +}); |
| 63 | + |
| 64 | +test.serial.cb(`should list sent Pub/Sub messages`, (t) => { |
| 65 | + requestObj |
| 66 | + .get(`/`) |
| 67 | + .expect(200) |
| 68 | + .expect((response) => { |
| 69 | + t.regex(response.text, /Messages received by this instance/); |
| 70 | + }) |
| 71 | + .end(t.end); |
| 72 | +}); |
0 commit comments