Skip to content

Commit 59d2e4f

Browse files
committed
Add unit test checking for non-query refType in executeQuery argument
1 parent 387bf8c commit 59d2e4f

File tree

1 file changed

+94
-0
lines changed

1 file changed

+94
-0
lines changed
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
/**
2+
* @license
3+
* Copyright 2024 Google LLC
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
import { initializeApp } from '@firebase/app';
19+
import { FirebaseAuthTokenData } from '@firebase/auth-interop-types';
20+
import { expect } from 'chai';
21+
import * as chai from 'chai';
22+
import chaiAsPromised from 'chai-as-promised';
23+
24+
import {
25+
DataConnectOptions,
26+
getDataConnect,
27+
MUTATION_STR,
28+
QUERY_STR,
29+
QueryRef
30+
} from '../../src';
31+
import { Code, DataConnectError } from '../../src/core/error';
32+
import {
33+
AuthTokenListener,
34+
AuthTokenProvider
35+
} from '../../src/core/FirebaseAuthProvider';
36+
import { QueryManager } from '../../src/core/QueryManager';
37+
import { RESTTransport } from '../../src/network/transport/rest';
38+
chai.use(chaiAsPromised);
39+
const options: DataConnectOptions = {
40+
connector: 'c',
41+
location: 'l',
42+
projectId: 'p',
43+
service: 's'
44+
};
45+
const INITIAL_TOKEN = 'initial token';
46+
class FakeAuthProvider implements AuthTokenProvider {
47+
private token: string | null = INITIAL_TOKEN;
48+
addTokenChangeListener(listener: AuthTokenListener): void {}
49+
getToken(forceRefresh: boolean): Promise<FirebaseAuthTokenData | null> {
50+
if (!forceRefresh) {
51+
return Promise.resolve({ accessToken: this.token! });
52+
}
53+
return Promise.resolve({ accessToken: 'testToken' });
54+
}
55+
setToken(_token: string | null): void {
56+
this.token = _token;
57+
}
58+
}
59+
60+
describe('Query Manager Tests', () => {
61+
it('should refuse to make requests to execute non-query operations', async () => {
62+
const authProvider = new FakeAuthProvider();
63+
const rt = new RESTTransport(options, undefined, undefined, authProvider);
64+
const qm = new QueryManager(rt);
65+
const app = initializeApp({ projectId: 'p' });
66+
const dc = getDataConnect(app, {
67+
connector: 'c',
68+
location: 'l',
69+
service: 's'
70+
});
71+
72+
const mutationRef: QueryRef<string, string> = {
73+
name: 'm',
74+
variables: 'v',
75+
dataConnect: dc,
76+
refType: MUTATION_STR as 'query'
77+
};
78+
79+
const queryRef: QueryRef<string, string> = {
80+
name: 'm',
81+
variables: 'v',
82+
dataConnect: dc,
83+
refType: QUERY_STR
84+
};
85+
86+
const error = new DataConnectError(
87+
Code.INVALID_ARGUMENT,
88+
`ExecuteQuery can only execute query operation`
89+
);
90+
91+
expect(() => qm.executeQuery(mutationRef)).to.throw(error.message);
92+
expect(() => qm.executeQuery(queryRef)).to.not.throw(error.message);
93+
});
94+
});

0 commit comments

Comments
 (0)