All files / utils/moderation access.js

92.3% Statements 12/13
81.81% Branches 9/11
100% Functions 4/4
92.3% Lines 12/13

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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 549x   9x           154x       110x                 261x 261x 19x   242x     242x       9x                     9x     9x              
const { authenticateRequest } = require('../../shared/auth-helper')
 
const CORS_HEADERS = {
  'Content-Type': 'application/json',
  'Access-Control-Allow-Origin': '*'
}
 
function errorResponse(statusCode, error, extra = {}) {
  return { statusCode, headers: CORS_HEADERS, body: JSON.stringify({ error, ...extra }) }
}
 
function jsonResponse(statusCode, body) {
  return { statusCode, headers: CORS_HEADERS, body: JSON.stringify(body) }
}
 
/**
 * Moderation is a personal activity of a signed-in user: no RBAC permission
 * (reviewing needs no membership anywhere), no API keys. An active flow is not
 * required either — reviewing is open to any user.
 */
async function authenticateModerator(event) {
  const auth = await authenticateRequest(event)
  if (!auth || auth.error) {
    return { response: errorResponse(401, auth?.error || 'Authentication required') }
  }
  Iif (!auth.userId) {
    return { response: errorResponse(401, 'Moderation requires a signed-in user') }
  }
  return { auth }
}
 
/** HTTP status of each refusal code the engine can return. */
const STATUS_BY_CODE = {
  INVALID_VOTE: 400,
  OWN_CONTENT: 403,
  NOT_FOUND: 404,
  TASK_DECIDED: 409,
  NOT_REVIEWABLE: 409,
  ALREADY_VOTED: 409
}
 
/** Turns an engine refusal `{ ok: false, code }` into an HTTP response. */
function refusalResponse(code) {
  return errorResponse(STATUS_BY_CODE[code] || 400, code)
}
 
module.exports = {
  authenticateModerator,
  errorResponse,
  jsonResponse,
  refusalResponse,
  CORS_HEADERS
}