summaryrefslogtreecommitdiff
path: root/jstests/core/api_version_parameters.js
blob: e370037b26fa209dd3bbd72ef0c136f54fc9df80 (plain)
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
/**
 * Checks that the server properly parses "API Version" parameters
 *
 * @tags: [
 *  requires_fcv_47,
 *  uses_api_parameters,
 * ]
 */

(function() {
"use strict";

// Test parsing logic on command included in API V1.
// If the client passed apiStrict, they must also pass apiVersion.
assert.commandFailedWithCode(db.runCommand({ping: 1, apiStrict: true}),
                             4886600,
                             "Provided apiStrict without passing apiVersion");

// If the client passed apiDeprecationErrors, they must also pass apiVersion.
assert.commandFailedWithCode(db.runCommand({ping: 1, apiDeprecationErrors: false}),
                             4886600,
                             "Provided apiDeprecationErrors without passing apiVersion");

// If the client passed apiVersion, it must be of type string.
assert.commandFailedWithCode(db.runCommand({ping: 1, apiVersion: 1}),
                             ErrorCodes.TypeMismatch,
                             "apiVersion' is the wrong type 'double', expected type 'string'");

// If the client passed apiVersion, its value must be "1".
assert.commandFailedWithCode(db.runCommand({ping: 1, apiVersion: "2"}),
                             ErrorCodes.APIVersionError,
                             "API version must be \"1\"");

// If the client passed apiStrict, it must be of type boolean.
assert.commandFailedWithCode(db.runCommand({ping: 1, apiVersion: "1", apiStrict: "true"}),
                             ErrorCodes.TypeMismatch,
                             "apiStrict' is the wrong type 'string', expected type 'boolean'");

// If the client passed apiDeprecationErrors, it must be of type boolean.
assert.commandFailedWithCode(
    db.runCommand({ping: 1, apiVersion: "1", apiDeprecationErrors: "false"}),
    ErrorCodes.TypeMismatch,
    "apiDeprecationErrors' is the wrong type 'string', expected type 'boolean'");

// Sanity check that command works with proper parameters.
assert.commandWorked(
    db.runCommand({ping: 1, apiVersion: "1", apiStrict: true, apiDeprecationErrors: true}));
assert.commandWorked(
    db.runCommand({ping: 1, apiVersion: "1", apiStrict: false, apiDeprecationErrors: false}));
assert.commandWorked(db.runCommand({ping: 1, apiVersion: "1"}));

// Test parsing logic on command not included in API V1.
assert.commandWorked(db.runCommand({listCommands: 1, apiVersion: "1"}));
// If the client passed apiStrict: true, but the command is not in V1, reply with
// APIStrictError.
assert.commandFailedWithCode(
    db.runCommand({listCommands: 1, apiVersion: "1", apiStrict: true}),
    ErrorCodes.APIStrictError,
    "Provided apiStrict: true, but the invoked command's apiVersions() does not include \"1\"");
assert.commandWorked(db.runCommand({listCommands: 1, apiVersion: "1", apiDeprecationErrors: true}));

// Test parsing logic of command deprecated in API V1.
assert.commandWorked(db.runCommand({testDeprecation: 1, apiVersion: "1"}));
assert.commandWorked(db.runCommand({testDeprecation: 1, apiVersion: "1", apiStrict: true}));
// If the client passed apiDeprecationErrors: true, but the command is
// deprecated in API Version 1, reply with APIDeprecationError.
assert.commandFailedWithCode(
    db.runCommand({testDeprecation: 1, apiVersion: "1", apiDeprecationErrors: true}),
    ErrorCodes.APIDeprecationError,
    "Provided apiDeprecationErrors: true, but the invoked command's deprecatedApiVersions() does not include \"1\"");

// Test writing to system.js fails.
assert.commandFailedWithCode(
    db.runCommand({
        insert: "system.js",
        documents: [{
            _id: "shouldntExist",
            value: function() {
                return 1;
            }
        }],
        apiVersion: "1",
        apiStrict: true
    }),
    ErrorCodes.APIStrictError,
    "Provided apiStrict:true, but the command insert attempts to write to system.js");
assert.commandFailedWithCode(
    db.runCommand({
        update: "system.js",
        updates: [{
            q: {
                _id: "shouldExist",
                value: function() {
                    return 1;
                }
            },
            u: {
                _id: "shouldExist",
                value: function() {
                    return 2;
                }
            }
        }],
        apiVersion: "1",
        apiStrict: true
    }),
    ErrorCodes.APIStrictError,
    "Provided apiStrict:true, but the command update attempts to write to system.js");
assert.commandFailedWithCode(
    db.runCommand({
        delete: "system.js",
        deletes: [{
            q: {
                _id: "shouldExist",
                value: function() {
                    return 1;
                }
            },
            limit: 1
        }],
        apiVersion: "1",
        apiStrict: true
    }),
    ErrorCodes.APIStrictError,
    "Provided apiStrict:true, but the command delete attempts to write to system.js");
assert.commandFailedWithCode(
    db.runCommand({
        findAndModify: "system.js",
        query: {
            _id: "shouldExist",
            value: function() {
                return 1;
            }
        },
        delete: true,
        apiVersion: "1",
        apiStrict: true
    }),
    ErrorCodes.APIStrictError,
    "Provided apiStrict:true, but the command findAndModify attempts to write to system.js");
// Test reading from system.js succeeds.
assert.commandWorked(db.runCommand(
    {find: "system.js", filter: {_id: "shouldExist"}, apiVersion: "1", apiStrict: true}));
})();