From aa7ec7d8be6e19251586453a46d1809f0a13f593 Mon Sep 17 00:00:00 2001 From: Moriz Wahl Date: Thu, 5 Aug 2021 23:31:40 +0200 Subject: [PATCH] login via graphql --- frontend/.env.dist | 2 +- frontend/src/config/index.js | 2 +- frontend/src/graphql/queries.js | 17 ++++++++++++++ frontend/src/views/Pages/Login.spec.js | 31 ++++++++++++++----------- frontend/src/views/Pages/Login.vue | 32 ++++++++++++++++---------- 5 files changed, 57 insertions(+), 27 deletions(-) create mode 100644 frontend/src/graphql/queries.js diff --git a/frontend/.env.dist b/frontend/.env.dist index 62ef359f3..a22b31307 100644 --- a/frontend/.env.dist +++ b/frontend/.env.dist @@ -1,4 +1,4 @@ LOGIN_API_URL=http://localhost/login_api/ COMMUNITY_API_URL=http://localhost/api/ ALLOW_REGISTER=true -GRAPHQL_URI=http://localhost:4000 +GRAPHQL_URI=http://localhost:4000/graphql diff --git a/frontend/src/config/index.js b/frontend/src/config/index.js index 1a4805cdf..dd35eb185 100644 --- a/frontend/src/config/index.js +++ b/frontend/src/config/index.js @@ -13,7 +13,7 @@ const environment = { const server = { LOGIN_API_URL: process.env.LOGIN_API_URL || 'http://localhost/login_api/', COMMUNITY_API_URL: process.env.COMMUNITY_API_URL || 'http://localhost/api/', - GRAPHQL_URI: process.env.GRAPHQL_URI || 'http://localhost:4000', + GRAPHQL_URI: process.env.GRAPHQL_URI || 'http://localhost:4000/graphql', } const CONFIG = { diff --git a/frontend/src/graphql/queries.js b/frontend/src/graphql/queries.js new file mode 100644 index 000000000..6a51b3858 --- /dev/null +++ b/frontend/src/graphql/queries.js @@ -0,0 +1,17 @@ +import gql from 'graphql-tag' + +export const login = gql` + query($email: String!, $password: String!) { + login(email: $email, password: $password) { + sessionId + user { + email + firstName + lastName + language + username + description + } + } + } +` diff --git a/frontend/src/views/Pages/Login.spec.js b/frontend/src/views/Pages/Login.spec.js index 8fe428020..7cbe5f003 100644 --- a/frontend/src/views/Pages/Login.spec.js +++ b/frontend/src/views/Pages/Login.spec.js @@ -1,18 +1,13 @@ import { mount, RouterLinkStub } from '@vue/test-utils' import flushPromises from 'flush-promises' -import loginAPI from '../../apis/loginAPI' import Login from './Login' -jest.mock('../../apis/loginAPI') - const localVue = global.localVue -const mockLoginCall = jest.fn() -mockLoginCall.mockReturnValue({ - success: true, - result: { - data: { - session_id: 1, +const loginQueryMock = jest.fn().mockResolvedValue({ + data: { + login: { + sessionId: 1, user: { name: 'Peter Lustig', }, @@ -20,8 +15,6 @@ mockLoginCall.mockReturnValue({ }, }) -loginAPI.login = mockLoginCall - const toastErrorMock = jest.fn() const mockStoreDispach = jest.fn() const mockRouterPush = jest.fn() @@ -52,6 +45,9 @@ describe('Login', () => { $toasted: { error: toastErrorMock, }, + $apollo: { + query: loginQueryMock, + }, } const stubs = { @@ -147,7 +143,14 @@ describe('Login', () => { }) it('calls the API with the given data', () => { - expect(mockLoginCall).toBeCalledWith('user@example.org', '1234') + expect(loginQueryMock).toBeCalledWith( + expect.objectContaining({ + variables: { + email: 'user@example.org', + password: '1234', + }, + }), + ) }) it('creates a spinner', () => { @@ -173,7 +176,9 @@ describe('Login', () => { describe('login fails', () => { beforeEach(() => { - mockLoginCall.mockReturnValue({ success: false }) + loginQueryMock.mockRejectedValue({ + message: 'Ouch!', + }) }) it('hides the spinner', () => { diff --git a/frontend/src/views/Pages/Login.vue b/frontend/src/views/Pages/Login.vue index 6d4b6d6e9..5d1072d45 100755 --- a/frontend/src/views/Pages/Login.vue +++ b/frontend/src/views/Pages/Login.vue @@ -55,10 +55,10 @@