Created
July 19, 2019 12:15
-
-
Save bafonins/8dc9d62866132d136cd2489643520fc0 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| const assert = require('assert') | |
| const TTN = require('ttn-lw').default | |
| const axios = require('axios') | |
| const is = process.env.TTN_LW_IDENTITY_SERVER_ADDRESS | |
| const token = process.env.access_token | |
| const ttnClient = new TTN( | |
| token, | |
| { | |
| stackConfig: { is: `${is}/api/v3` }, | |
| connectionType: 'http', | |
| proxy: false, | |
| } | |
| ) | |
| const adminId = 'admin' | |
| const userId = 'test-scrip-user' | |
| const userEmail = '[email protected]' | |
| const userId2 = 'test-script-user2' | |
| const userEmail2 = '[email protected]' | |
| const organizationId = 'test-script-organization'; | |
| (async function () { | |
| try { | |
| await axios.post(`${is}/api/v3/users`, { | |
| user: { | |
| ids: { user_id: userId }, | |
| password: 'Test123!@#', | |
| password_confirm: 'Test123!@#', | |
| primary_email_address: userEmail, | |
| }, | |
| }) | |
| console.log(`created user:${userId}`) | |
| await axios.post(`${is}/api/v3/users`, { | |
| user: { | |
| ids: { user_id: userId2 }, | |
| password: 'Test123!@#', | |
| password_confirm: 'Test123!@#', | |
| primary_email_address: userEmail2, | |
| }, | |
| }) | |
| console.log(`created user:${userId2}`) | |
| await ttnClient.api.OrganizationRegistry.Create( | |
| { routeParams: { 'collaborator.user_ids.user_id': adminId }}, | |
| { | |
| organization: { | |
| ids: { | |
| organization_id: organizationId, | |
| }, | |
| }, | |
| }) | |
| console.log(`created organization:${organizationId}`) | |
| await ttnClient.api.OrganizationAccess.SetCollaborator({ | |
| routeParams: { 'organization_ids.organization_id': organizationId }, | |
| }, | |
| { | |
| collaborator: { | |
| ids: { user_ids: { user_id: userId2 }}, | |
| rights: [ 'RIGHT_ALL' ], | |
| }, | |
| }) | |
| console.log(`added ${userId2} as a collaborator to organization ${organizationId}`) | |
| const app = await ttnClient.Applications.create(adminId, { ids: { application_id: 'test-app' }}) | |
| const appId = app.ids.application_id | |
| console.log(`created application:${appId}`) | |
| { | |
| console.log('get(Collaborator)ByUserId') | |
| const collaboratorAdmin = await ttnClient.Applications.Collaborators.getByUserId(appId, adminId) | |
| console.log(collaboratorAdmin) | |
| assert(adminId === collaboratorAdmin.ids.user_ids.user_id) | |
| } | |
| await ttnClient.Applications.Collaborators.add(appId, { | |
| ids: { user_ids: { user_id: userId }}, | |
| rights: [ 'RIGHT_ALL' ], | |
| }) | |
| console.log(`added ${userId} as a collaborator to application ${appId}`) | |
| { | |
| console.log('get(Collaborator)ByUserId') | |
| const collaboratorUser = await ttnClient.Applications.Collaborators.getByUserId(appId, userId) | |
| console.log('received collaborator:', collaboratorUser) | |
| assert(userId === collaboratorUser.ids.user_ids.user_id) | |
| } | |
| await ttnClient.Applications.Collaborators.add(appId, { | |
| ids: { organization_ids: { organization_id: organizationId }}, | |
| rights: [ 'RIGHT_ALL' ], | |
| }) | |
| console.log(`added ${organizationId} as a collaborator to application ${appId}`) | |
| { | |
| console.log('get(Collaborator)ByOrganizationId') | |
| const collaboratorOrganization = await ttnClient.Applications.Collaborators.getByOrganizationId(appId, organizationId) | |
| console.log('received collaborator:', collaboratorOrganization) | |
| assert(organizationId === collaboratorOrganization.ids.organization_ids.organization_id) | |
| } | |
| const gtw = await ttnClient.Gateways.create(adminId, { | |
| enforce_duty_cycle: true, | |
| frequency_plan_id: 'EU_863_870', | |
| gateway_server_address: 'localhost:1885', | |
| ids: { gateway_id: 'test-gtw' }, | |
| }) | |
| const gtwId = gtw.ids.gateway_id | |
| console.log(`created gateway:${gtwId}`) | |
| { | |
| console.log('get(Collaborator)ByUserId') | |
| const collaboratorAdmin = await ttnClient.Gateways.Collaborators.getByUserId(gtwId, adminId) | |
| console.log('received collaborator:', collaboratorAdmin) | |
| assert(adminId === collaboratorAdmin.ids.user_ids.user_id) | |
| } | |
| await ttnClient.Gateways.Collaborators.add(gtwId, { | |
| ids: { user_ids: { user_id: userId }}, | |
| rights: [ 'RIGHT_ALL' ], | |
| }) | |
| console.log(`added ${userId} as a collaborator to gateway ${gtwId}`) | |
| { | |
| console.log('get(Collaborator)ByUserId') | |
| const collaboratorUser = await ttnClient.Gateways.Collaborators.getByUserId(gtwId, userId) | |
| console.log('received collaborator:', collaboratorUser) | |
| assert(userId === collaboratorUser.ids.user_ids.user_id) | |
| } | |
| await ttnClient.Gateways.Collaborators.add(gtwId, { | |
| ids: { organization_ids: { organization_id: organizationId }}, | |
| rights: [ 'RIGHT_ALL' ], | |
| }) | |
| console.log(`added ${organizationId} as a collaborator to gateway ${gtwId}`) | |
| { | |
| console.log('get(Collaborator)ByOrganizationId') | |
| const collaboratorOrganization = await ttnClient.Gateways.Collaborators.getByOrganizationId(gtwId, organizationId) | |
| console.log('received collaborator:', collaboratorOrganization) | |
| assert(organizationId === collaboratorOrganization.ids.organization_ids.organization_id) | |
| } | |
| } catch (error) { | |
| console.log('error:', error) | |
| } | |
| } | |
| )() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment