AppSync - Cognito: user/pwd not working in jest test - rejectNoUserPool thrown

0

Hello,

I have an AppSync app configured with Cognito authentication. All is working fine for the Amplify frontend. I would like to add some jest integration tests and was hoping to be able to also use user/pwd authentication (found examples using API_KEY but that is not really something I want to add)

I have tried a lot of things and config options but all of them result in

  console.error
    [ERROR] 18:24.753 AuthError -
                Error: Amplify has not been configured correctly.
                The configuration object is missing required auth properties.
                This error is typically caused by one of the following scenarios:

                1. Did you run `amplify push` after adding auth via `amplify add auth`?
                    See https://aws-amplify.github.io/docs/js/authentication#amplify-project-setup for more information

                2. This could also be caused by multiple conflicting versions of amplify packages, see (https://docs.amplify.aws/lib/troubleshooting/upgrading/q/platform/js) for help upgrading Amplify packages.



      at ConsoleLogger.Object.<anonymous>.ConsoleLogger._log (node_modules/@aws-amplify/core/src/Logger/ConsoleLogger.ts:115:4)
      at ConsoleLogger.Object.<anonymous>.ConsoleLogger.error (node_modules/@aws-amplify/core/src/Logger/ConsoleLogger.ts:174:12)
      at NoUserPoolError.AuthError [as constructor] (node_modules/@aws-amplify/auth/src/Errors.ts:34:10)
      at new NoUserPoolError (node_modules/@aws-amplify/auth/src/Errors.ts:40:3)
      at AuthClass.Object.<anonymous>.AuthClass.rejectNoUserPool (node_modules/@aws-amplify/auth/src/Auth.ts:2248:25)
      at AuthClass.Object.<anonymous>.AuthClass.signIn (node_modules/@aws-amplify/auth/src/Auth.ts:480:16)
      at signIn (test/AnnotationsSpec.ts:44:17)

Before sharing any of the config details (which I am sure would be needed to provide detailed help) is it at all possible to use Appsync with USER_PWD authentication from a jest test? Any examples 'out there'?

Tx!!

Peter

PS using a bunch of curl commands it does work out to send a query to the AppSync app and get the results back

gefragt vor 2 Jahren990 Aufrufe
1 Antwort
0

Eventually got it working

import Amplify, {Auth, API } from 'aws-amplify';
import {GraphQLResult} from '@aws-amplify/api-graphql';
...
Amplify.configure({
  aws_appsync_graphqlEndpoint: process.env.GRAPHQLURL,
  Auth: {
    region: process.env.AWS_DEFAULT_REGION,
    identityPoolRegion: process.env.AWS_DEFAULT_REGION,
    userPoolId: process.env.USERPOOLID,
    userPoolWebClientId: process.env.CLIENTID,
    Credentials: {
      authenticationFlowType: 'USER_PASSWORD_AUTH',
    }
  }
});

...

const token = await Auth.signIn( process.env.USERID || '', process.env.APPSYNCPWD);
...
const response = (await API.graphql({ query , authMode: 'AMAZON_COGNITO_USER_POOLS', authToken: token} )) as GraphQLResult;
...

Looks more complicated than I would hope is needed, but for now I am going to stick with it

Suggestions to simplify more than welcome!

Tx

Peter

beantwortet vor 2 Jahren

Du bist nicht angemeldet. Anmelden um eine Antwort zu veröffentlichen.

Eine gute Antwort beantwortet die Frage klar, gibt konstruktives Feedback und fördert die berufliche Weiterentwicklung des Fragenstellers.

Richtlinien für die Beantwortung von Fragen