Skip to content
This repository has been archived by the owner on Mar 22, 2019. It is now read-only.

[WIP] add addSocialMediaAccount mutation #233

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/jwt/decode.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ export default async (driver, authorizationHeader) => {
try {
const decoded = await jwt.verify(token, process.env.JWT_SECRET)
id = decoded.sub
} catch {
} catch (e) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

e could also mean event, as a good practice use err or error instead.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I will change that, and how was that merged to master with a syntax error merged.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That’s a really good question.

return null
}
const session = driver.session()
Expand Down
2 changes: 2 additions & 0 deletions src/middleware/permissionsMiddleware.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ const permissions = shield({
CreateBadge: isAdmin,
UpdateBadge: isAdmin,
DeleteBadge: isAdmin,
addSocialMedia: isAuthenticated,

// addFruitToBasket: isAuthenticated
follow: isAuthenticated,
unfollow: isAuthenticated,
Expand Down
29 changes: 24 additions & 5 deletions src/resolvers/user_management.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import encode from '../jwt/encode'
import bcrypt from 'bcryptjs'
import { AuthenticationError } from 'apollo-server'
import { neo4jgraphql } from 'neo4j-graphql-js'
import encode from "../jwt/encode"
import bcrypt from "bcryptjs"
import { AuthenticationError } from "apollo-server"
import { neo4jgraphql } from "neo4j-graphql-js"

export default {
Query: {
Expand All @@ -25,7 +25,7 @@ export default {

return true
},
login: async (parent, { email, password }, { driver, req, user }) => {
login: async (_, { email, password }, { driver, req, user }) => {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What renaming the parent?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we are not using it, so I thought I could use a convention that I have seen, where the _ indicates that I'm not going to use that parameter

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it also helps with the linter

// if (user && user.id) {
// throw new Error('Already logged in.')
// }
Expand Down Expand Up @@ -100,6 +100,25 @@ export default {

return encode(currentUser)
}
},
addSocialMedia: async (_, { url }, { driver, user }) => {
const session = driver.session()

const { email } = user
const result = await session.run(
`MATCH (user:User {email: $userEmail})
SET user.socialMedia = [$url]
RETURN user {.socialMedia}`,
{
userEmail: email,
url
}
)
session.close()
const [currentUser] = result.records.map(record => {
return record.get("user")
})
return !!currentUser.socialMedia
}
}
}
66 changes: 33 additions & 33 deletions src/resolvers/user_management.spec.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import Factory from '../seed/factories'
import { GraphQLClient, request } from 'graphql-request'
import jwt from 'jsonwebtoken'
import { host, login } from '../jest/helpers'
import Factory from "../seed/factories";
import { GraphQLClient, request } from "graphql-request";
import jwt from "jsonwebtoken";
import { host, login } from "../jest/helpers";

const factory = Factory()
const factory = Factory();

// here is the decoded JWT token:
// {
Expand Down Expand Up @@ -117,37 +117,37 @@ describe('currentUser', () => {
email
role
}
}`
}`;

describe('unauthenticated', () => {
it('returns null', async () => {
const expected = { currentUser: null }
await expect(request(host, query)).resolves.toEqual(expected)
})
})
describe("unauthenticated", () => {
it("returns null", async () => {
const expected = { currentUser: null };
await expect(request(host, query)).resolves.toEqual(expected);
});
});

describe('with valid JWT Bearer Token', () => {
let client
let headers
describe("with valid JWT Bearer Token", () => {
let client;
let headers;

describe('but no corresponding user in the database', () => {
describe("but no corresponding user in the database", () => {
beforeEach(async () => {
client = new GraphQLClient(host, { headers: jennyRostocksHeaders })
})
client = new GraphQLClient(host, { headers: jennyRostocksHeaders });
});

it('returns null', async () => {
const expected = { currentUser: null }
await expect(client.request(query)).resolves.toEqual(expected)
})
})
it("returns null", async () => {
const expected = { currentUser: null };
await expect(client.request(query)).resolves.toEqual(expected);
});
});

describe('and corresponding user in the database', () => {
describe("and corresponding user in the database", () => {
beforeEach(async () => {
headers = await login({ email: '[email protected]', password: '1234' })
client = new GraphQLClient(host, { headers })
})
headers = await login({ email: "[email protected]", password: "1234" });
client = new GraphQLClient(host, { headers });
});

it('returns the whole user object', async () => {
it("returns the whole user object", async () => {
const expected = {
currentUser: {
avatar: 'https://s3.amazonaws.com/uifaces/faces/twitter/jimmuirhead/128.jpg',
Expand Down Expand Up @@ -185,11 +185,11 @@ describe('login', () => {
)
const token = data.login
jwt.verify(token, process.env.JWT_SECRET, (err, data) => {
expect(data.email).toEqual('[email protected]')
expect(err).toBeNull()
})
})
})
expect(data.email).toEqual("[email protected]");
expect(err).toBeNull();
});
});
});

describe('valid email/password but user is disabled', () => {
it('responds with "Your account has been disabled."', async () => {
Expand Down
8 changes: 8 additions & 0 deletions src/schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ type Mutation {
report(id: ID!, description: String): Report
disable(id: ID!): ID
enable(id: ID!): ID
addSocialMedia(url: String!): Boolean!
"Shout the given Type and ID"
shout(id: ID!, type: ShoutTypeEnum): Boolean! @cypher(statement: """
MATCH (n {id: $id})<-[:WROTE]-(wu:User), (u:User {id: $cypherParams.currentUserId})
Expand Down Expand Up @@ -118,6 +119,7 @@ type User {
location: Location @cypher(statement: "MATCH (this)-[:IS_IN]->(l:Location) RETURN l")
locationName: String
about: String
socialMedia: [String]

createdAt: String
updatedAt: String
Expand Down Expand Up @@ -298,3 +300,9 @@ type SharedInboxEndpoint {
id: ID!
uri: String
}

type socialMedia {
id: ID!
name: String!
icon: String!
}