Skip to content

Commit 13f96b1

Browse files
committed
Merge branch 'dev' of https://github.com/oslabs-beta/ksqljs into mx/jestTestsBranch
2 parents 8954293 + 8d10b18 commit 13f96b1

File tree

9 files changed

+350
-314
lines changed

9 files changed

+350
-314
lines changed

.gitignore

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,4 +104,11 @@ dist
104104
.tern-port
105105

106106
# Local
107-
./ksqljsTest.js
107+
ksqljsTest.js
108+
package-lock.json
109+
local_ignore/
110+
.gitignore
111+
112+
# KSQLDB docker server settings
113+
ksqldb_server_config/
114+

__tests__/integrationtests.js

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
const ksqljs = require('../ksqljs/ksqlJS.js');
22

33
// Pre-requisite: start a docker container
4+
/* To add to README: Prior to running test with 'npm test', please start the ksqlDB
5+
server using the command 'docker compose-up'. This will spin up a ksqlDB server on
6+
'http://localhost:8088'
7+
*/
48

59
describe('--Integration Tests--', () => {
610

@@ -61,7 +65,8 @@ describe('--Integration Tests--', () => {
6165

6266
it('.pull receives the correct data from a pull query', async () => {
6367
const pullData = await client.pull("SELECT * FROM TESTJESTSTREAM;");
64-
expect(pullData[1]).toEqual(['stab-rabbit', '123@mail.com', 100]);
68+
console.log(pullData[1]);
69+
expect(pullData[1]).toEqual(["stab-rabbit", "123@mail.com", 100]);
6570
})
6671

6772
it('.pullFromTo receives all the data', async () => {
@@ -182,4 +187,4 @@ describe('--Integration Tests--', () => {
182187
// }));
183188
// })
184189
})
185-
})
190+
})

__tests__/builderTest.js renamed to __tests__/queryBuilderTests.js

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
const queryBuilder = require('../ksqljs/queryBuilder.js');
2+
const { QueryBuilderError, EmptyQueryError, NumParamsError, InappropriateStringParamError } = require('../ksqljs/customErrors.js');
3+
24

35
describe('Unit tests for query builder class', () => {
46
let builder;
@@ -18,33 +20,34 @@ describe('Unit tests for query builder class', () => {
1820
});
1921

2022
describe('Error testing', () => {
21-
it('returns an error if number of params is different from number of question marks', () => {
22-
const finishedQuery = builder.build(query, 123, "middle", "extra");
23-
expect(finishedQuery).toBeInstanceOf(Error);
23+
it('throws an error if number of params is different from number of question marks', () => {
24+
expect(() => { builder.build(query, 123, "middle", "extra") }).toThrow(NumParamsError);
2425
});
2526

26-
it('returns an error if the query is empty', () => {
27+
it('throws an error if the query is empty', () => {
2728
const query = '';
28-
const finishedQuery = builder.build(query, 123);
29-
expect(finishedQuery).toBeInstanceOf(Error);
29+
expect(() => { builder.build(query, 123) }).toThrow(EmptyQueryError);
3030
});
3131

32-
it('returns an error if an object is passed in as a param', () => {
33-
const finishedQuery = builder.build(query, 123, { "middle": "size" });
34-
expect(finishedQuery).toBeInstanceOf(Error);
32+
it('throws an error if an object is passed in as a param', () => {
33+
expect(() => { builder.build(query, 123, { "middle": "size" }) }).toThrow(QueryBuilderError);
3534
});
3635
});
3736

3837
describe('SQL injection', () => {
3938
it("prevents 'OR 1=1' SQL injection by escaping single quotations ", () => {
4039
// https://stackoverflow.com/questions/5139770/escape-character-in-sql-server
41-
const finishedQuery = builder.build(query, 123, "middle' OR 1=1",);
40+
const finishedQuery = builder.build(query, 123, "middle' OR 1=1");
4241
expect(finishedQuery).toEqual("SELECT * FROM table WHERE id = 123 AND size = 'middle'' OR 1=1'");
4342
});
4443

4544
it("prevents (middle' OR 'a'=a') SQL injection by escaping single quotations ", () => {
4645
const finishedQuery = builder.build(query, 123, "middle' OR 'a'='a",);
4746
expect(finishedQuery).toEqual("SELECT * FROM table WHERE id = 123 AND size = 'middle'' OR ''a''=''a'");
4847
});
48+
49+
it("throws an error if user tries to add a semicolon into a string param not wrapped in quotes", () => {
50+
expect(() => { builder.build(query, ['123; DROP tables WHERE size = '], 'middle') }).toThrow(InappropriateStringParamError);
51+
});
4952
});
5053
});

docker-compose.yml

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,10 @@ services:
3434
image: confluentinc/ksqldb-server:0.25.1
3535
hostname: ksqldb-server
3636
container_name: ksqldb-server
37+
volumes:
38+
- type: bind
39+
source: ./
40+
target: /home/appuser
3741
depends_on:
3842
- broker
3943
ports:
@@ -43,6 +47,16 @@ services:
4347
KSQL_BOOTSTRAP_SERVERS: broker:9092
4448
KSQL_KSQL_LOGGING_PROCESSING_STREAM_AUTO_CREATE: "true"
4549
KSQL_KSQL_LOGGING_PROCESSING_TOPIC_AUTO_CREATE: "true"
50+
# KSQL_KSQL_OPTS: "-Djava.security.auth.login.config=/jaas_config.file"
51+
# KSQL_AUTHENTICATION_METHOD: BASIC
52+
# KSQL_AUTHENTICATION_REALM: KsqlServer-Props
53+
# KSQL_AUTHENTICATION_ROLES: admin,developer,user
54+
# KSQL_SSL_CLIENT_AUTHENTICATION: NONE
55+
# KSQL_SSL_TRUSTSTORE_LOCATION: ksqldb_server_config/kafka.server.truststore.jks
56+
# KSQL_SSL_TRUSTSTORE_PASSWORD: ${SSL_PASSWORD}
57+
# KSQL_SSL_KEYSTORE_LOCATION: ksqldb_server_config/kafka.server.keystore.jks
58+
# KSQL_SSL_KEYSTORE_PASSWORD: ${SSL_PASSWORD}
59+
# KSQL_SSL_KEY_PASSWORD: ${SSL_PASSWORD}
4660
KSQL_KSQL_HEARTBEAT_ENABLE: "true"
4761
KSQL_KSQL_LAG_REPORTING_ENABLE: "true"
4862

ksqljs/customErrors.js

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
/** This file contains custom Node.js errors that extends the built-in Error class
2+
* REF: https://rclayton.silvrback.com/custom-errors-in-node-js
3+
* REF: https://futurestud.io/tutorials/node-js-create-your-custom-error
4+
*/
5+
6+
// used to wrap error received from ksqlDB server
7+
class ksqlDBError extends Error {
8+
constructor(error) {
9+
super(error.message)
10+
11+
// Ensure the name of this error is the same as the class name
12+
this.name = this.constructor.name
13+
14+
// capturing the stack trace keeps the reference to your error class
15+
Error.captureStackTrace(this, this.constructor);
16+
17+
// you may also assign additional properties to your error
18+
//this.status = 404
19+
}
20+
}
21+
22+
// for returning error related to use of queryBuilder class
23+
class QueryBuilderError extends Error {
24+
constructor(message) {
25+
super(message)
26+
this.name = this.constructor.name
27+
Error.captureStackTrace(this, this.constructor);
28+
}
29+
}
30+
31+
class EmptyQueryError extends QueryBuilderError {
32+
constructor() {
33+
super('Query should not be empty, undefined, or null');
34+
}
35+
}
36+
37+
class NumParamsError extends QueryBuilderError {
38+
constructor(message) {
39+
super(message);
40+
}
41+
}
42+
43+
class InappropriateStringParamError extends QueryBuilderError {
44+
constructor(message) {
45+
super(message);
46+
}
47+
}
48+
49+
module.exports = {
50+
ksqlDBError,
51+
QueryBuilderError,
52+
EmptyQueryError,
53+
NumParamsError,
54+
InappropriateStringParamError
55+
};

0 commit comments

Comments
 (0)