-
Notifications
You must be signed in to change notification settings - Fork 5
/
test.js
89 lines (74 loc) · 2.63 KB
/
test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
var graphql = require('graphql');
var noIntrospection = require('./index');
var expect = require('chai').expect;
var express = require('express');
var request = require('supertest-as-promised');
var graphqlExpress = require('graphql-server-express').graphqlExpress;
var bodyParser = require('body-parser');
var schema = new graphql.GraphQLSchema({
query: new graphql.GraphQLObjectType({
name: 'Query',
fields: {
hello: {
type: graphql.GraphQLString,
resolve() {
return Promise.resolve('world');
}
}
}
})
});
describe('NoIntrospection validation rule', function(){
it('disallows queries with __schema', function(){
var query = graphql.parse('{ __schema { queryType { name } } }');
var validationErrors = graphql.validate(schema, query, [noIntrospection]);
return expect(validationErrors[0].message).to.match(/introspection is not allowed/);
});
it('disallows queries with __type', function(){
var query = graphql.parse('{ __type(name: "Query"){ name } }');
var validationErrors = graphql.validate(schema, query, [noIntrospection]);
return expect(validationErrors[0].message).to.match(/introspection is not allowed/);
});
it('allows valid queries that do not contain __schema or __type', function(){
var query = graphql.parse('{ hello }');
var validationErrors = graphql.validate(schema, query, [noIntrospection]);
return expect(validationErrors.length).to.equal(0);
});
describe('works with graphql-server-express', function() {
var app = express();
app.use('/graphql',
bodyParser.json(),
graphqlExpress({ schema: schema, validationRules: [noIntrospection] })
);
it('disables introspection using __schema', function() {
var req = request(app)
.post('/graphql')
.send({
query: '{ __schema { queryType { name } } }'
});
return req.then( function(result) {
return expect(result.body.errors[0].message).to.match(/introspection is not allowed/);
});
});
it('disables introspection using __type', function() {
var req = request(app)
.post('/graphql')
.send({
query: '{ __type(name: "Query") { name } }'
});
return req.then( function(result) {
return expect(result.body.errors[0].message).to.match(/introspection is not allowed/);
});
});
it('allows other valid queries through', function() {
var req = request(app)
.post('/graphql')
.send({
query: '{ hello }'
});
return req.then( function(result) {
return expect(result.body).to.deep.equal({ data: { hello: 'world' } });
});
});
});
});