Improve: Add more API tests (#20738)
parent
54d782b9e0
commit
6e1bc0bfaa
@ -0,0 +1,192 @@ |
||||
import { expect } from 'chai'; |
||||
|
||||
import { getCredentials, api, request, credentials } from '../../data/api-data.js'; |
||||
|
||||
describe('push token', function() { |
||||
this.retries(0); |
||||
|
||||
before((done) => getCredentials(done)); |
||||
|
||||
describe('POST [/push.token]', () => { |
||||
it('should fail if not logged in', (done) => { |
||||
request.post(api('push.token')) |
||||
.expect(401) |
||||
.expect((res) => { |
||||
expect(res.body).to.have.property('status', 'error'); |
||||
expect(res.body).to.have.property('message'); |
||||
}) |
||||
.end(done); |
||||
}); |
||||
|
||||
it('should fail if missing type', (done) => { |
||||
request.post(api('push.token')) |
||||
.set(credentials) |
||||
.send({ |
||||
value: 'token', |
||||
appName: 'com.example.rocketchat', |
||||
}) |
||||
.expect(400) |
||||
.expect((res) => { |
||||
expect(res.body).to.have.property('success', false); |
||||
expect(res.body).to.have.property('errorType', 'error-type-param-not-valid'); |
||||
}) |
||||
.end(done); |
||||
}); |
||||
|
||||
it('should fail if missing value', (done) => { |
||||
request.post(api('push.token')) |
||||
.set(credentials) |
||||
.send({ |
||||
type: 'gcm', |
||||
appName: 'com.example.rocketchat', |
||||
}) |
||||
.expect(400) |
||||
.expect((res) => { |
||||
expect(res.body).to.have.property('success', false); |
||||
expect(res.body).to.have.property('errorType', 'error-token-param-not-valid'); |
||||
}) |
||||
.end(done); |
||||
}); |
||||
|
||||
it('should fail if missing appName', (done) => { |
||||
request.post(api('push.token')) |
||||
.set(credentials) |
||||
.send({ |
||||
type: 'gcm', |
||||
value: 'token', |
||||
}) |
||||
.expect(400) |
||||
.expect((res) => { |
||||
expect(res.body).to.have.property('success', false); |
||||
expect(res.body).to.have.property('errorType', 'error-appName-param-not-valid'); |
||||
}) |
||||
.end(done); |
||||
}); |
||||
|
||||
it('should fail if type param is unknown', (done) => { |
||||
request.post(api('push.token')) |
||||
.set(credentials) |
||||
.send({ |
||||
type: 'unknownPlatform', |
||||
}) |
||||
.expect(400) |
||||
.expect((res) => { |
||||
expect(res.body).to.have.property('success', false); |
||||
expect(res.body).to.have.property('errorType', 'error-type-param-not-valid'); |
||||
}) |
||||
.end(done); |
||||
}); |
||||
|
||||
it('should fail if token param is empty', (done) => { |
||||
request.post(api('push.token')) |
||||
.set(credentials) |
||||
.send({ |
||||
type: 'gcm', |
||||
appName: 'com.example.rocketchat', |
||||
value: '', |
||||
}) |
||||
.expect(400) |
||||
.expect((res) => { |
||||
expect(res.body).to.have.property('success', false); |
||||
expect(res.body).to.have.property('errorType', 'error-token-param-not-valid'); |
||||
}) |
||||
.end(done); |
||||
}); |
||||
|
||||
it('should add a token if valid', (done) => { |
||||
request.post(api('push.token')) |
||||
.set(credentials) |
||||
.send({ |
||||
type: 'gcm', |
||||
value: 'token', |
||||
appName: 'com.example.rocketchat', |
||||
}) |
||||
.expect(200) |
||||
.expect((res) => { |
||||
expect(res.body).to.have.property('success', true); |
||||
expect(res.body).to.have.property('result').and.to.be.an('object'); |
||||
}) |
||||
.end(done); |
||||
}); |
||||
}); |
||||
|
||||
describe('DELETE [/push.token]', () => { |
||||
it('should fail if not logged in', (done) => { |
||||
request.delete(api('push.token')) |
||||
.send({ |
||||
token: 'token', |
||||
}) |
||||
.expect(401) |
||||
.expect((res) => { |
||||
expect(res.body).to.have.property('status', 'error'); |
||||
expect(res.body).to.have.property('message'); |
||||
}) |
||||
.end(done); |
||||
}); |
||||
|
||||
it('should fail if missing token key', (done) => { |
||||
request.delete(api('push.token')) |
||||
.set(credentials) |
||||
.send({}) |
||||
.expect(400) |
||||
.expect((res) => { |
||||
expect(res.body).to.have.property('success', false); |
||||
expect(res.body).to.have.property('errorType', 'error-token-param-not-valid'); |
||||
}) |
||||
.end(done); |
||||
}); |
||||
|
||||
it('should fail if token is empty', (done) => { |
||||
request.delete(api('push.token')) |
||||
.set(credentials) |
||||
.send({ |
||||
token: '', |
||||
}) |
||||
.expect(400) |
||||
.expect((res) => { |
||||
expect(res.body).to.have.property('success', false); |
||||
expect(res.body).to.have.property('errorType', 'error-token-param-not-valid'); |
||||
}) |
||||
.end(done); |
||||
}); |
||||
|
||||
it('should fail if token is invalid', (done) => { |
||||
request.delete(api('push.token')) |
||||
.set(credentials) |
||||
.send({ |
||||
token: '123', |
||||
}) |
||||
.expect(404) |
||||
.expect((res) => { |
||||
expect(res.body).to.have.property('success', false); |
||||
}) |
||||
.end(done); |
||||
}); |
||||
|
||||
it('should delete a token if valid', (done) => { |
||||
request.delete(api('push.token')) |
||||
.set(credentials) |
||||
.send({ |
||||
token: 'token', |
||||
}) |
||||
.expect(200) |
||||
.expect((res) => { |
||||
expect(res.body).to.have.property('success', true); |
||||
}) |
||||
.end(done); |
||||
}); |
||||
|
||||
it('should fail if token is already deleted', (done) => { |
||||
request.delete(api('push.token')) |
||||
.set(credentials) |
||||
.send({ |
||||
token: 'token', |
||||
}) |
||||
.expect(404) |
||||
.expect((res) => { |
||||
expect(res.body).to.have.property('success', false); |
||||
}) |
||||
.end(done); |
||||
}); |
||||
}); |
||||
}); |
@ -0,0 +1,230 @@ |
||||
import { expect } from 'chai'; |
||||
|
||||
import { getCredentials, api, request, credentials } from '../../data/api-data.js'; |
||||
|
||||
describe('Invites', function() { |
||||
let testInviteID; |
||||
this.retries(0); |
||||
|
||||
before((done) => getCredentials(done)); |
||||
describe('POST [/findOrCreateInvite]', () => { |
||||
it('should fail if not logged in', (done) => { |
||||
request.post(api('findOrCreateInvite')) |
||||
.send({ |
||||
rid: 'GENERAL', |
||||
days: 1, |
||||
maxUses: 10, |
||||
}) |
||||
.expect(401) |
||||
.expect((res) => { |
||||
expect(res.body).to.have.property('status', 'error'); |
||||
expect(res.body).to.have.property('message'); |
||||
}) |
||||
.end(done); |
||||
}); |
||||
|
||||
it('should fail if invalid roomid', (done) => { |
||||
request.post(api('findOrCreateInvite')) |
||||
.set(credentials) |
||||
.send({ |
||||
rid: 'invalid', |
||||
days: 1, |
||||
maxUses: 10, |
||||
}) |
||||
.expect(400) |
||||
.expect((res) => { |
||||
expect(res.body).to.have.property('success', false); |
||||
expect(res.body).to.have.property('errorType', 'error-invalid-room'); |
||||
}) |
||||
.end(done); |
||||
}); |
||||
|
||||
it('should create an invite for GENERAL', (done) => { |
||||
request.post(api('findOrCreateInvite')) |
||||
.set(credentials) |
||||
.send({ |
||||
rid: 'GENERAL', |
||||
days: 1, |
||||
maxUses: 10, |
||||
}) |
||||
.expect(200) |
||||
.expect((res) => { |
||||
expect(res.body).to.have.property('success', true); |
||||
expect(res.body).to.have.property('days', 1); |
||||
expect(res.body).to.have.property('maxUses', 10); |
||||
expect(res.body).to.have.property('uses'); |
||||
expect(res.body).to.have.property('_id'); |
||||
testInviteID = res.body._id; |
||||
}) |
||||
.end(done); |
||||
}); |
||||
|
||||
it('should return an existing invite for GENERAL', (done) => { |
||||
request.post(api('findOrCreateInvite')) |
||||
.set(credentials) |
||||
.send({ |
||||
rid: 'GENERAL', |
||||
days: 1, |
||||
maxUses: 10, |
||||
}) |
||||
.expect(200) |
||||
.expect((res) => { |
||||
expect(res.body).to.have.property('success', true); |
||||
expect(res.body).to.have.property('days', 1); |
||||
expect(res.body).to.have.property('maxUses', 10); |
||||
expect(res.body).to.have.property('uses'); |
||||
expect(res.body).to.have.property('_id', testInviteID); |
||||
}) |
||||
.end(done); |
||||
}); |
||||
}); |
||||
|
||||
describe('GET [/listInvites]', () => { |
||||
it('should fail if not logged in', (done) => { |
||||
request.get(api('listInvites')) |
||||
.expect(401) |
||||
.expect((res) => { |
||||
expect(res.body).to.have.property('status', 'error'); |
||||
expect(res.body).to.have.property('message'); |
||||
}) |
||||
.end(done); |
||||
}); |
||||
|
||||
it('should return the existing invite for GENERAL', (done) => { |
||||
request.get(api('listInvites')) |
||||
.set(credentials) |
||||
.expect(200) |
||||
.expect((res) => { |
||||
expect(res.body[0]).to.have.property('_id', testInviteID); |
||||
}) |
||||
.end(done); |
||||
}); |
||||
}); |
||||
|
||||
describe('POST [/useInviteToken]', () => { |
||||
it('should fail if not logged in', (done) => { |
||||
request.post(api('useInviteToken')) |
||||
.expect(401) |
||||
.expect((res) => { |
||||
expect(res.body).to.have.property('status', 'error'); |
||||
expect(res.body).to.have.property('message'); |
||||
}) |
||||
.end(done); |
||||
}); |
||||
|
||||
it('should fail if invalid token', (done) => { |
||||
request.post(api('useInviteToken')) |
||||
.set(credentials) |
||||
.send({ |
||||
token: 'invalid', |
||||
}) |
||||
.expect(400) |
||||
.expect((res) => { |
||||
expect(res.body).to.have.property('success', false); |
||||
expect(res.body).to.have.property('errorType', 'error-invalid-token'); |
||||
}) |
||||
.end(done); |
||||
}); |
||||
|
||||
it('should fail if missing token', (done) => { |
||||
request.post(api('useInviteToken')) |
||||
.set(credentials) |
||||
.send({ |
||||
}) |
||||
.expect(400) |
||||
.expect((res) => { |
||||
expect(res.body).to.have.property('success', false); |
||||
expect(res.body).to.have.property('errorType', 'error-invalid-token'); |
||||
}) |
||||
.end(done); |
||||
}); |
||||
|
||||
it('should use the existing invite for GENERAL', (done) => { |
||||
request.post(api('useInviteToken')) |
||||
.set(credentials) |
||||
.send({ |
||||
token: testInviteID, |
||||
}) |
||||
.expect(200) |
||||
.expect((res) => { |
||||
expect(res.body).to.have.property('success', true); |
||||
}) |
||||
.end(done); |
||||
}); |
||||
}); |
||||
|
||||
describe('POST [/validateInviteToken]', () => { |
||||
it('should warn if invalid token', (done) => { |
||||
request.post(api('validateInviteToken')) |
||||
.set(credentials) |
||||
.send({ |
||||
token: 'invalid', |
||||
}) |
||||
.expect(200) |
||||
.expect((res) => { |
||||
expect(res.body).to.have.property('success', true); |
||||
expect(res.body).to.have.property('valid', false); |
||||
}) |
||||
.end(done); |
||||
}); |
||||
|
||||
it('should succeed when valid token', (done) => { |
||||
request.post(api('validateInviteToken')) |
||||
.set(credentials) |
||||
.send({ |
||||
token: testInviteID, |
||||
}) |
||||
.expect(200) |
||||
.expect((res) => { |
||||
expect(res.body).to.have.property('success', true); |
||||
expect(res.body).to.have.property('valid', true); |
||||
}) |
||||
.end(done); |
||||
}); |
||||
}); |
||||
|
||||
describe('DELETE [/removeInvite]', () => { |
||||
it('should fail if not logged in', (done) => { |
||||
request.delete(api(`removeInvite/${ testInviteID }`)) |
||||
.expect(401) |
||||
.expect((res) => { |
||||
expect(res.body).to.have.property('status', 'error'); |
||||
expect(res.body).to.have.property('message'); |
||||
}) |
||||
.end(done); |
||||
}); |
||||
|
||||
|
||||
it('should fail if invalid token', (done) => { |
||||
request.delete(api('removeInvite/invalid')) |
||||
.set(credentials) |
||||
.expect(400) |
||||
.expect((res) => { |
||||
expect(res.body).to.have.property('success', false); |
||||
expect(res.body).to.have.property('errorType', 'invalid-invitation-id'); |
||||
}) |
||||
.end(done); |
||||
}); |
||||
|
||||
it('should succeed when valid token', (done) => { |
||||
request.delete(api(`removeInvite/${ testInviteID }`)) |
||||
.set(credentials) |
||||
.expect(200) |
||||
.expect((res) => { |
||||
expect(res.body).to.equal(true); |
||||
}) |
||||
.end(done); |
||||
}); |
||||
|
||||
it('should fail when deleting the same invite again', (done) => { |
||||
request.delete(api(`removeInvite/${ testInviteID }`)) |
||||
.set(credentials) |
||||
.expect(400) |
||||
.expect((res) => { |
||||
expect(res.body).to.have.property('success', false); |
||||
expect(res.body).to.have.property('errorType', 'invalid-invitation-id'); |
||||
}) |
||||
.end(done); |
||||
}); |
||||
}); |
||||
}); |
@ -0,0 +1,192 @@ |
||||
import { expect } from 'chai'; |
||||
|
||||
import { getCredentials, request, methodCall, api, credentials } from '../../data/api-data.js'; |
||||
|
||||
describe('Meteor.methods', function() { |
||||
this.retries(0); |
||||
|
||||
before((done) => getCredentials(done)); |
||||
|
||||
describe('[@loadMissedMessages]', () => { |
||||
let rid = false; |
||||
const date = { |
||||
$date: new Date().getTime(), |
||||
}; |
||||
let postMessageDate = false; |
||||
|
||||
const channelName = `methods-test-channel-${ Date.now() }`; |
||||
|
||||
before('@loadMissedMessages', (done) => { |
||||
request.post(api('groups.create')) |
||||
.set(credentials) |
||||
.send({ |
||||
name: channelName, |
||||
}) |
||||
.expect('Content-Type', 'application/json') |
||||
.expect(200) |
||||
.expect((res) => { |
||||
expect(res.body).to.have.property('success', true); |
||||
expect(res.body).to.have.nested.property('group._id'); |
||||
expect(res.body).to.have.nested.property('group.name', channelName); |
||||
expect(res.body).to.have.nested.property('group.t', 'p'); |
||||
expect(res.body).to.have.nested.property('group.msgs', 0); |
||||
rid = res.body.group._id; |
||||
}) |
||||
.end(done); |
||||
}); |
||||
|
||||
before('@loadMissedMessages', (done) => { |
||||
request.post(api('chat.sendMessage')) |
||||
.set(credentials) |
||||
.send({ |
||||
message: { |
||||
text: 'Sample message', |
||||
rid, |
||||
}, |
||||
}) |
||||
.expect('Content-Type', 'application/json') |
||||
.expect(200) |
||||
.expect((res) => { |
||||
expect(res.body).to.have.property('success', true); |
||||
postMessageDate = { $date: new Date().getTime() }; |
||||
}) |
||||
.end(done); |
||||
}); |
||||
|
||||
before('@loadMissedMessages', (done) => { |
||||
request.post(api('chat.sendMessage')) |
||||
.set(credentials) |
||||
.send({ |
||||
message: { |
||||
text: 'Second Sample message', |
||||
rid, |
||||
}, |
||||
}) |
||||
.expect('Content-Type', 'application/json') |
||||
.expect(200) |
||||
.expect((res) => { |
||||
expect(res.body).to.have.property('success', true); |
||||
}) |
||||
.end(done); |
||||
}); |
||||
|
||||
it('should fail if not logged in', (done) => { |
||||
request.post(methodCall('loadMissedMessages')) |
||||
.send({ |
||||
message: JSON.stringify({ |
||||
method: 'loadMissedMessages', |
||||
params: [rid, date], |
||||
}), |
||||
}) |
||||
.expect('Content-Type', 'application/json') |
||||
.expect(401) |
||||
.expect((res) => { |
||||
expect(res.body).to.have.property('status', 'error'); |
||||
expect(res.body).to.have.property('message'); |
||||
}) |
||||
.end(done); |
||||
}); |
||||
|
||||
it('should return an error if the rid param is empty', (done) => { |
||||
request.post(methodCall('loadMissedMessages')) |
||||
.set(credentials) |
||||
.send({ |
||||
message: JSON.stringify({ |
||||
method: 'loadMissedMessages', |
||||
params: ['', date], |
||||
}), |
||||
}) |
||||
.expect('Content-Type', 'application/json') |
||||
.expect(200) |
||||
.expect((res) => { |
||||
expect(res.body).to.have.a.property('success', true); |
||||
expect(res.body).to.have.a.property('message').that.include('error-invalid-room'); |
||||
}) |
||||
.end(done); |
||||
}); |
||||
|
||||
it('should return an error if the start param is missing', (done) => { |
||||
request.post(methodCall('loadMissedMessages')) |
||||
.set(credentials) |
||||
.send({ |
||||
message: JSON.stringify({ |
||||
method: 'loadMissedMessages', |
||||
params: [rid], |
||||
}), |
||||
}) |
||||
.expect('Content-Type', 'application/json') |
||||
.expect(200) |
||||
.expect((res) => { |
||||
expect(res.body).to.have.a.property('success', true); |
||||
expect(res.body).to.have.a.property('message').that.include('Match error'); |
||||
}) |
||||
.end(done); |
||||
}); |
||||
|
||||
it('should return and empty list if using current time', (done) => { |
||||
request.post(methodCall('loadMissedMessages')) |
||||
.set(credentials) |
||||
.send({ |
||||
message: JSON.stringify({ |
||||
method: 'loadMissedMessages', |
||||
params: [rid, { $date: new Date().getTime() }], |
||||
}), |
||||
}) |
||||
.expect('Content-Type', 'application/json') |
||||
.expect(200) |
||||
.expect((res) => { |
||||
expect(res.body).to.have.a.property('success', true); |
||||
expect(res.body).to.have.a.property('message').that.is.a('string'); |
||||
|
||||
const data = JSON.parse(res.body.message); |
||||
expect(data).to.have.a.property('result').that.is.a('array'); |
||||
expect(data.result.length).to.be.equal(0); |
||||
}) |
||||
.end(done); |
||||
}); |
||||
|
||||
it('should return two messages if using a time from before the first msg was sent', (done) => { |
||||
request.post(methodCall('loadMissedMessages')) |
||||
.set(credentials) |
||||
.send({ |
||||
message: JSON.stringify({ |
||||
method: 'loadMissedMessages', |
||||
params: [rid, date], |
||||
}), |
||||
}) |
||||
.expect('Content-Type', 'application/json') |
||||
.expect(200) |
||||
.expect((res) => { |
||||
expect(res.body).to.have.a.property('success', true); |
||||
expect(res.body).to.have.a.property('message').that.is.a('string'); |
||||
|
||||
const data = JSON.parse(res.body.message); |
||||
expect(data).to.have.a.property('result').that.is.a('array'); |
||||
expect(data.result.length).to.be.equal(2); |
||||
}) |
||||
.end(done); |
||||
}); |
||||
|
||||
it('should return a single message if using a time from in between the messages', (done) => { |
||||
request.post(methodCall('loadMissedMessages')) |
||||
.set(credentials) |
||||
.send({ |
||||
message: JSON.stringify({ |
||||
method: 'loadMissedMessages', |
||||
params: [rid, postMessageDate], |
||||
}), |
||||
}) |
||||
.expect('Content-Type', 'application/json') |
||||
.expect(200) |
||||
.expect((res) => { |
||||
expect(res.body).to.have.a.property('success', true); |
||||
expect(res.body).to.have.a.property('message').that.is.a('string'); |
||||
|
||||
const data = JSON.parse(res.body.message); |
||||
expect(data).to.have.a.property('result').that.is.a('array'); |
||||
expect(data.result.length).to.be.equal(1); |
||||
}) |
||||
.end(done); |
||||
}); |
||||
}); |
||||
}); |
Loading…
Reference in new issue