Skip to content

Commit 5ab5614

Browse files
committed
add integration tests
1 parent a5928f3 commit 5ab5614

File tree

5 files changed

+198
-1
lines changed

5 files changed

+198
-1
lines changed

spec/integration.spec.js

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
const Parse = require('parse/node');
2+
const { TestUtils } = require('parse-server');
3+
const { PARSE_APP_ID, PARSE_MASTER_KEY, reconfigureServer, serverURL } = require('./mocks/server');
4+
const { httpRequest } = require('./support/request');
5+
6+
const fileData = 'hello world';
7+
8+
describe('S3Adapter integration tests', () => {
9+
beforeEach(async () => {
10+
process.env.TESTING = true;
11+
12+
await reconfigureServer();
13+
14+
Parse.initialize(PARSE_APP_ID);
15+
Parse.serverURL = serverURL;
16+
Parse.CoreManager.set('SERVER_URL', serverURL);
17+
Parse.CoreManager.set('MASTER_KEY', PARSE_MASTER_KEY);
18+
}, 60 * 1000);
19+
20+
afterAll(async () => {
21+
Parse.Storage._clear();
22+
await TestUtils.destroyAllDataPermanently(true);
23+
});
24+
25+
it('should create a file in Parse Server', async () => {
26+
const fileName = 'test-1.txt';
27+
28+
const base64 = Buffer.from(fileData).toString('base64');
29+
const file = new Parse.File(fileName, { base64 });
30+
31+
await file.save();
32+
33+
expect(file).toBeDefined();
34+
expect(file.url()).toContain(fileName);
35+
});
36+
37+
it(
38+
'should read the contents of the file',
39+
async () => {
40+
const fileName = 'test-2.txt';
41+
const base64 = Buffer.from(fileData).toString('base64');
42+
const file = new Parse.File(fileName, { base64 });
43+
await file.save();
44+
const fileLink = file.url();
45+
46+
const response = await httpRequest(fileLink);
47+
const text = response.toString();
48+
49+
expect(text).toBe(fileData); // Check if the contents match the original data
50+
},
51+
60 * 1000
52+
);
53+
54+
it(
55+
'should delete the file',
56+
async () => {
57+
const fileName = 'test-3.txt';
58+
59+
const base64 = Buffer.from(fileData).toString('base64');
60+
const file = new Parse.File(fileName, { base64 });
61+
await file.save();
62+
63+
const fileLink = file.url();
64+
await file.destroy();
65+
66+
return expectAsync(httpRequest(fileLink)).toBeRejectedWithError(
67+
'Request failed with status code 404'
68+
);
69+
},
70+
60 * 1000
71+
);
72+
});
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
module.exports = (options = {}) => {
2+
const adapter = {
3+
sendVerificationEmail: () => Promise.resolve(),
4+
sendPasswordResetEmail: () => Promise.resolve(),
5+
sendMail: () => Promise.resolve(),
6+
};
7+
if (options.sendMail) {
8+
adapter.sendMail = options.sendMail;
9+
}
10+
if (options.sendPasswordResetEmail) {
11+
adapter.sendPasswordResetEmail = options.sendPasswordResetEmail;
12+
}
13+
if (options.sendVerificationEmail) {
14+
adapter.sendVerificationEmail = options.sendVerificationEmail;
15+
}
16+
17+
return adapter;
18+
};

spec/mocks/s3adapter.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,4 +74,4 @@ function makeS3Adapter(options) {
7474
return s3;
7575
}
7676

77-
module.exports = {makeS3Adapter}
77+
module.exports = { makeS3Adapter };

spec/mocks/server.js

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
const { ParseServer } = require('parse-server');
2+
const MockEmailAdapterWithOptions = require('./MockEmailAdapterWithOptions');
3+
const { makeS3Adapter } = require('./s3adapter');
4+
5+
const port = 1327;
6+
const mountPath = '/api/parse';
7+
const serverURL = 'http://127.0.0.1:1327/api/parse';
8+
9+
const PARSE_APP_ID = 'app-id';
10+
const PARSE_MASTER_KEY = 'master-key';
11+
12+
const S3Adapter = makeS3Adapter();
13+
14+
const defaultConfiguration = {
15+
databaseURI: 'mongodb://127.0.0.1:27017/s3-adapter',
16+
appId: PARSE_APP_ID,
17+
masterKey: PARSE_MASTER_KEY,
18+
serverURL,
19+
liveQuery: {
20+
classNames: [],
21+
},
22+
startLiveQueryServer: true,
23+
verbose: false,
24+
silent: true,
25+
fileUpload: {
26+
enableForPublic: true,
27+
enableForAnonymousUser: true,
28+
enableForAuthenticatedUser: true,
29+
},
30+
revokeSessionOnPasswordReset: false,
31+
allowCustomObjectId: false,
32+
allowClientClassCreation: true,
33+
encodeParseObjectInCloudFunction: true,
34+
masterKeyIps: ['0.0.0.0/0', '0.0.0.0', '::/0', '::'],
35+
emailAdapter: MockEmailAdapterWithOptions(),
36+
port,
37+
mountPath,
38+
filesAdapter: S3Adapter,
39+
};
40+
41+
let parseServer;
42+
43+
const reconfigureServer = async () => {
44+
if (parseServer) {
45+
await parseServer.handleShutdown();
46+
await new Promise(resolve => parseServer.server.close(resolve));
47+
parseServer = undefined;
48+
return reconfigureServer();
49+
}
50+
51+
parseServer = await ParseServer.startApp(defaultConfiguration);
52+
if (parseServer.config.state === 'initialized') {
53+
console.error('Failed to initialize Parse Server');
54+
return reconfigureServer();
55+
}
56+
57+
return parseServer;
58+
};
59+
60+
module.exports = {
61+
reconfigureServer,
62+
S3Adapter,
63+
port,
64+
mountPath,
65+
serverURL,
66+
PARSE_APP_ID,
67+
PARSE_MASTER_KEY,
68+
};

spec/support/request.js

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
const http = require('http');
2+
const https = require('https');
3+
4+
/**
5+
* Makes an HTTP or HTTPS request.
6+
* @param {string} url - The URL to request.
7+
* @returns {Promise<string>} - A promise that resolves with the response data or rejects with an error.
8+
*/
9+
function httpRequest(url) {
10+
return new Promise((resolve, reject) => {
11+
// Determine the appropriate module to use based on the URL protocol
12+
const client = url.startsWith('https') ? https : http;
13+
14+
// Make the request
15+
client
16+
.get(url, response => {
17+
let data = '';
18+
19+
// Collect the data chunks
20+
response.on('data', chunk => {
21+
data += chunk;
22+
});
23+
24+
// When the response ends, resolve or reject the promise
25+
response.on('end', () => {
26+
if (response.statusCode >= 200 && response.statusCode < 300) {
27+
resolve(data); // Resolve with the collected data
28+
} else {
29+
reject(new Error(`Request failed with status code ${response.statusCode}`));
30+
}
31+
});
32+
})
33+
.on('error', error => {
34+
reject(new Error(`Error making request: ${error.message}`)); // Reject on error
35+
});
36+
});
37+
}
38+
39+
module.exports = { httpRequest };

0 commit comments

Comments
 (0)