Commit cbe8ed9f authored by Frank Bessou's avatar Frank Bessou 🍁
Browse files

feat: create a token for use in test on startup

parent 36bbf69ff523
......@@ -43,19 +43,27 @@ describe('CwSimpleHtttpClient Tests', function() {
});
describe('CwSigningHttpClient Tests', function() {
it('should be instantiable', function() {
const url = 'http://www.cubicweb.org';
const token =
'ffb90d39c9a848319742232302613d9e049882befc7c414ebb1d8d846e4bcac3c044cf5bf6bd4995a99711f888e86d2f2f7f6cbea4cb41e7acba68266586e0be';
const client = new CwSigningHttpClient(url, 'runner-6', token);
expect(url + '/').toBe(client.baseUrl);
async function getAuthToken() {
const response = await fetch(`${CW_BASE_URL}/admin-token`);
const token = await response.json();
console.log(token);
return token;
}
it('should be instantiable', async function() {
const token = await getAuthToken();
const client = new CwSigningHttpClient(
CW_BASE_URL,
'admin-token',
token
);
expect(CW_BASE_URL + '/').toBe(client.baseUrl);
});
it('should handle RQL queries', async () => {
const token =
'ffb90d39c9a848319742232302613d9e049882befc7c414ebb1d8d846e4bcac3c044cf5bf6bd4995a99711f888e86d2f2f7f6cbea4cb41e7acba68266586e0be';
const token = await getAuthToken();
const client = new CwRqlClient(
new CwSigningHttpClient(CW_BASE_URL, 'runner-6', token)
new CwSigningHttpClient(CW_BASE_URL, 'admin-token', token)
);
await client
.transactionV1([['Any U WHERE U login %(l)s', {l: 'admin'}]])
......
"""cubicweb-test-server application package
"""
def includeme(config):
config.include("cubicweb_test_server.admin_token")
from pyramid.view import view_config
def get_admin_token(request):
with request.cw_cnx.repo.internal_cnx() as cnx:
token = cnx.execute(
"""
Any
AUTH_TOKEN
WHERE
AUTH_TOKEN id 'admin-token',
AUTH_TOKEN token_for_user ADMIN,
ADMIN login 'admin'
"""
).one()
return token.token
def includeme(config):
config.add_route("admin-token", "/admin-token")
config.add_view(get_admin_token, route_name="admin-token", renderer="json")
from cubicweb.server.hook import Hook
class CreateAuthTokenStartupHook(Hook):
"""on startup, create a auth token for the admin"""
__regid__ = "test_server.create_auth_token"
events = ("server_startup",)
def create_token(self, cnx):
admin = cnx.find("CWUser", login="admin").one()
token = cnx.create_entity(
"AuthToken", token_for_user=admin, id="admin-token", enabled=True
)
cnx.commit()
print(f"Created token {token.id} - {token.token}")
def __call__(self):
if self.repo.config.creating:
return
with self.repo.internal_cnx() as cnx:
if cnx.find("AuthToken", id="admin-token"):
# The token already exists
return
self.create_token(cnx)
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment