All files / app/assets/javascripts/captcha apollo_captcha_link.js

100% Statements 16/16
66.67% Branches 2/3
100% Functions 7/7
100% Lines 15/15

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    342x 109x 5x       6x 5x 2x 2x   2x 2x 2x       1x           1x     1x 1x         3x      
import { ApolloLink, Observable } from 'apollo-link';
 
export const apolloCaptchaLink = new ApolloLink((operation, forward) =>
  forward(operation).flatMap((result) => {
    const { errors = [] } = result;
 
    // Our API will return with a top-level GraphQL error with extensions
    // in case a captcha is required.
    const captchaError = errors.find((e) => e?.extensions?.needs_captcha_response);
    if (captchaError) {
      const captchaSiteKey = captchaError.extensions.captcha_site_key;
      const spamLogId = captchaError.extensions.spam_log_id;
 
      return new Observable((observer) => {
        import('~/captcha/wait_for_captcha_to_be_solved')
          .then(({ waitForCaptchaToBeSolved }) => waitForCaptchaToBeSolved(captchaSiteKey))
          .then((captchaResponse) => {
            // If the captcha was solved correctly, we re-do our action while setting
            // captcha response headers.
            operation.setContext({
              headers: {
                'X-GitLab-Captcha-Response': captchaResponse,
                'X-GitLab-Spam-Log-Id': spamLogId,
              },
            });
            forward(operation).subscribe(observer);
          })
          .catch((error) => {
            observer.error(error);
            observer.complete();
          });
      });
    }
 
    return Observable.of(result);
  }),
);