Skip to content

Commit d13a7dd

Browse files
committed
Adds Session Tests
This commit adds session tests to the project. The additional session tests ensure that FileMaker sessions are properly handled by the client. This commit closes issue #66. Signed-off-by: Lui de la Parra <Lui@mutesymphony.com>
1 parent c8d3d42 commit d13a7dd

File tree

3 files changed

+209
-0
lines changed

3 files changed

+209
-0
lines changed

test/admin/index.js

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
const axios = require('axios');
2+
const path = require('path');
3+
const environment = require('dotenv');
4+
const varium = require('varium');
5+
const manifestPath = path.join(__dirname, '../env.manifest');
6+
7+
environment.config({ path: './test/.env' });
8+
varium({ manifestPath });
9+
10+
const instance = axios.create({
11+
baseURL: process.env.SERVER
12+
});
13+
14+
let adminToken = false;
15+
16+
const login = () =>
17+
instance
18+
.post(
19+
'/fmi/admin/api/v2/user/auth',
20+
{},
21+
{
22+
auth: {
23+
username: process.env.ADMIN_USER,
24+
password: process.env.ADMIN_PASSWORD
25+
}
26+
}
27+
)
28+
.then(response => {
29+
adminToken = response.data.response.token;
30+
return response.data.response.token;
31+
});
32+
33+
const logout = (token = adminToken) => instance
34+
.delete(`/fmi/admin/api/v2/user/auth/${token}`, {})
35+
36+
const remove = ({ id }, token = adminToken) =>
37+
instance
38+
.delete(`/fmi/admin/api/v2/clients/${id}`, {
39+
params: {
40+
graceTime: 0
41+
},
42+
headers: { Authorization: `Bearer ${token}` }
43+
})
44+
.then(response => response.data)
45+
.catch(error => console.log(error.response.data));
46+
47+
const drop = account =>
48+
find(account).then(sessions => {
49+
const removals = [];
50+
sessions.forEach(session => removals.push(remove(session)));
51+
return Promise.all(removals);
52+
});
53+
54+
const find = ({ userName }, token = adminToken) =>
55+
instance
56+
.get('/fmi/admin/api/v2/clients', {
57+
headers: { Authorization: `Bearer ${token}` }
58+
})
59+
.then(response =>
60+
response.data.response.clients.filter(
61+
client => client.userName === userName
62+
)
63+
);
64+
65+
module.exports = { admin: { login, logout, sessions: { find, drop } } };

test/env.manifest

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,11 @@ PASSWORD : String
77
LAYOUT : String
88
CONTAINER_LAYOUT : String
99

10+
# FileMaker Server Configuration
11+
12+
ADMIN_USER: String
13+
ADMIN_PASSWORD: String
14+
1015
# Client Configuration
1116

1217
CLIENT_NAME: String | star-wars-fan

test/session.test.js

Lines changed: 139 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,139 @@
1+
'use strict';
2+
3+
/* global describe before after beforeEach afterEach it */
4+
5+
/* eslint-disable */
6+
7+
const assert = require('assert');
8+
const { expect, should } = require('chai');
9+
const { admin } = require('./admin');
10+
11+
/* eslint-enable */
12+
13+
const path = require('path');
14+
const chai = require('chai');
15+
const chaiAsPromised = require('chai-as-promised');
16+
const environment = require('dotenv');
17+
const varium = require('varium');
18+
const { connect } = require('marpat');
19+
const { Filemaker } = require('../index.js');
20+
21+
const manifestPath = path.join(__dirname, './env.manifest');
22+
23+
chai.use(chaiAsPromised);
24+
25+
describe('Session Capabilities', () => {
26+
let database;
27+
let client;
28+
const name = 'testing-client';
29+
30+
before(done => {
31+
environment.config({ path: './test/.env' });
32+
varium({ manifestPath });
33+
connect('mongodb://127.0.0.1:27017/dapi')
34+
.then(db => {
35+
database = db;
36+
return database.dropDatabase();
37+
})
38+
.then(() => {
39+
client = Filemaker.create({
40+
name,
41+
database: process.env.DATABASE,
42+
server: process.env.SERVER,
43+
user: process.env.USERNAME,
44+
concurrency: 1,
45+
password: process.env.PASSWORD
46+
});
47+
return client.save();
48+
})
49+
.then(client => admin.login())
50+
.then(() => admin.sessions.drop({ userName: process.env.USERNAME }))
51+
.then(() => new Promise(resolve => setTimeout(() => done(), 12000)));
52+
});
53+
54+
beforeEach(done => {
55+
admin
56+
.login()
57+
.then(() => done())
58+
.catch(error => done(new Error(error.response.data.messages[0].text)));
59+
});
60+
61+
afterEach(done => {
62+
admin
63+
.logout()
64+
.then(() => done())
65+
.catch(error => done(new Error(error.response.data.messages[0].text)));
66+
});
67+
68+
after(done => {
69+
client
70+
.reset()
71+
.then(response => done())
72+
.catch(error => done(error));
73+
});
74+
75+
it('should create a client session', () => {
76+
return expect(
77+
Filemaker.findOne({ name }).then(client =>
78+
client
79+
.login()
80+
.then(
81+
token =>
82+
new Promise(resolve => setTimeout(() => resolve(token), 12000))
83+
)
84+
.then(response =>
85+
admin.sessions.find({ userName: process.env.USERNAME })
86+
)
87+
)
88+
)
89+
.to.eventually.be.a('array')
90+
.to.have.a.lengthOf(1);
91+
});
92+
93+
it('should reuse a client session', () => {
94+
return expect(
95+
Filemaker.findOne({ name })
96+
.then(client => client.list(process.env.LAYOUT))
97+
.then(
98+
response =>
99+
new Promise(resolve => setTimeout(() => resolve(response), 12000))
100+
)
101+
.then(response =>
102+
admin.sessions.find({ userName: process.env.USERNAME })
103+
)
104+
)
105+
.to.eventually.be.a('array')
106+
.to.have.a.lengthOf(1);
107+
});
108+
109+
it('should automatically remove an invalid session', () => {
110+
return expect(
111+
admin.sessions
112+
.drop({ userName: process.env.USERNAME })
113+
.then(() => new Promise(resolve => setTimeout(() => resolve(), 12000)))
114+
.then(() => Filemaker.findOne({ name }))
115+
.then(client => client.list(process.env.LAYOUT).catch(error => error))
116+
.then(() => client.agent.connection.sessions)
117+
)
118+
.to.eventually.be.a('array')
119+
.to.have.a.lengthOf(0);
120+
});
121+
122+
it('should automatically create a new session when required', () => {
123+
return expect(
124+
admin.sessions
125+
.drop({ userName: process.env.USERNAME })
126+
.then(
127+
token =>
128+
new Promise(resolve => setTimeout(() => resolve(token), 12000))
129+
)
130+
.then(() =>
131+
Filemaker.findOne({ name }).then(client =>
132+
client.list(process.env.LAYOUT)
133+
)
134+
)
135+
)
136+
.to.eventually.be.a('object')
137+
.that.has.all.keys('data', 'dataInfo');
138+
});
139+
});

0 commit comments

Comments
 (0)