safetrustcr / Backend

Supports a decentralized platform for secure and trusted P2P cryptocurrency transactions.
3 stars 9 forks source link

Set up Karate Testing Framework for GraphQL API - Users Module #29

Open rvalenciano opened 20 hours ago

rvalenciano commented 20 hours ago

Set up automated testing for our Hasura GraphQL API using Karate framework, focusing initially on the users and authentication functionality.

Follow this tutorial

Problem

We need to:

Solution

A possible solution looks like:

Project Structure

/tests
├── docker-compose.test.yml
├── karate-config.js
├── logback-test.xml
├── karate/
│   ├── features/
│   │   ├── auth/
│   │   │   ├── login.feature
│   │   │   └── permissions.feature
│   │   └── users/
│   │       ├── create.feature
│   │       ├── query.feature
│   │       └── wallets.feature
│   └── helpers/
│       └── schemas.js
└── results/

Example Test Feature

# karate/features/users/query.feature
Feature: User Queries

Background:
  * url HASURA_ENDPOINT
  * def query = read('queries/list-users.graphql')

Scenario: Query Current User
  Given header Authorization = 'Bearer ' + firebase_token
  And header x-hasura-role = 'user'
  And request { 
    query: 'query GetCurrentUser { users_by_pk(id: "' + user_id + '") { id email last_seen } }'
  }
  When method post
  Then status 200
  And match response.data.users_by_pk.id == user_id
  And match response.errors == '#notpresent'

Scenario: Query User Wallets
  Given header Authorization = 'Bearer ' + firebase_token
  And def query = read('queries/get-user-wallets.graphql')
  And def variables = { userId: '#(user_id)' }
  And request { query: '#(query)', variables: '#(variables)' }
  When method post
  Then status 200
  And match response.data.user_wallets[0] contains { is_primary: true }
  And match response.errors == '#notpresent'

Example GraphQL Queries

# karate/features/users/queries/get-user-wallets.graphql
query GetUserWallets($userId: String!) {
  user_wallets(
    where: {user_id: {_eq: $userId}}
    order_by: {is_primary: desc}
  ) {
    id
    wallet_address
    chain_type
    is_primary
    created_at
  }
}

Test Cases to Implement

User Authentication

User Queries

User Roles

User Wallets

Error Scenarios

Setup Instructions

Docker Configuration

version: '3.8'
services:
  karate:
    image: karate-test
    build:
      context: .
      dockerfile: Dockerfile.test
    volumes:
      - ./karate:/karate
      - ./results:/results
    environment:
      HASURA_ENDPOINT: http://graphql-engine:8080/v1/graphql
      HASURA_ADMIN_SECRET: ${HASURA_ADMIN_SECRET}
      FIREBASE_API_KEY: ${FIREBASE_API_KEY}
    depends_on:
      - graphql-engine

Karate Config with Firebase Support

// karate-config.js
function fn() {
  var env = karate.env || 'dev';
  var config = {
    env: env,
    HASURA_ENDPOINT: java.lang.System.getenv('HASURA_ENDPOINT'),
    HASURA_ADMIN_SECRET: java.lang.System.getenv('HASURA_ADMIN_SECRET'),
    FIREBASE_API_KEY: java.lang.System.getenv('FIREBASE_API_KEY')
  };

  // Setup Firebase test tokens
  if (env == 'dev') {
    // Add test user tokens
    config.tenant_token = 'test-tenant-token';
    config.landlord_token = 'test-landlord-token';
  }

  return config;
}

Success Criteria

Prerequisites

Benjtalkshow commented 19 hours ago

I am a Full Stack Developer with a strong background in blockchain and extensive experience in Next.js/React, TypeScript, and Rust. I’ve made over 45 contributions to over 15 projects in the OnlyDust ecosystem, solving complex issues and delivering efficient, scalable solutions.

Can i handle this task? i can take care of it.

sasasamaes commented 18 hours ago

I would love to help with this task! I am a sensei of a developer community called dev.f and a maintainer of revolutionary farmer and I have 5 years of experience as a full stack developer, I have experience in Javascript, Cairo, Python. This task seems like a great opportunity to learn a little more about docker, hasura and if I find it challenging: this is my github: https://github.com/sasasamaes