Skip to content

Commit

Permalink
test: initiate api config in tests with bearer token, remove unnecess…
Browse files Browse the repository at this point in the history
…ary api config inits on all integration tests
  • Loading branch information
g-saracca committed Dec 6, 2024
1 parent db44740 commit 448ed61
Show file tree
Hide file tree
Showing 9 changed files with 15 additions and 404 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,6 @@ import chai from 'chai'
import chaiAsPromised from 'chai-as-promised'
import { UserJSDataverseRepository } from '../../../../src/users/infrastructure/repositories/UserJSDataverseRepository'
import { TestsUtils } from '../../shared/TestsUtils'
import { ApiConfig } from '@iqss/dataverse-client-javascript'
import { DataverseApiAuthMechanism } from '@iqss/dataverse-client-javascript/dist/core/infra/repositories/ApiConfig'
import { DATAVERSE_BACKEND_URL, OIDC_AUTH_CONFIG } from '@/config'

chai.use(chaiAsPromised)
const expect = chai.expect
Expand All @@ -23,41 +20,17 @@ describe('API Token Info JSDataverse Repository', () => {
})

it('revoke the API token', async () => {
// Change the api config to use bearer token
ApiConfig.init(
`${DATAVERSE_BACKEND_URL}/api/v1`,
DataverseApiAuthMechanism.BEARER_TOKEN,
undefined,
`${OIDC_AUTH_CONFIG.LOCAL_STORAGE_KEY_PREFIX}token`
)

await expect(userRepository.deleteApiToken()).to.be.fulfilled
})

it('create or recreate the API token and return the new token info', async () => {
// Change the api config to use bearer token
ApiConfig.init(
`${DATAVERSE_BACKEND_URL}/api/v1`,
DataverseApiAuthMechanism.BEARER_TOKEN,
undefined,
`${OIDC_AUTH_CONFIG.LOCAL_STORAGE_KEY_PREFIX}token`
)

const recreatedTokenInfo = await userRepository.recreateApiToken()

expect(recreatedTokenInfo).to.have.property('apiToken').that.is.a('string')
expect(recreatedTokenInfo).to.have.property('expirationDate').that.is.a('Date')
})

it('fetch the current API token', async () => {
// Change the api config to use bearer token
ApiConfig.init(
`${DATAVERSE_BACKEND_URL}/api/v1`,
DataverseApiAuthMechanism.BEARER_TOKEN,
undefined,
`${OIDC_AUTH_CONFIG.LOCAL_STORAGE_KEY_PREFIX}token`
)

const tokenInfo = await userRepository.getCurrentApiToken()

expect(tokenInfo).to.have.property('apiToken').that.is.a('string')
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,6 @@ import {
UpwardHierarchyNode
} from '../../../../src/shared/hierarchy/domain/models/UpwardHierarchyNode'
import { Collection } from '../../../../src/collection/domain/models/Collection'
import { DATAVERSE_BACKEND_URL, OIDC_AUTH_CONFIG } from '@/config'
import {
ApiConfig,
DataverseApiAuthMechanism
} from '@iqss/dataverse-client-javascript/dist/core/infra/repositories/ApiConfig'

const collectionRepository = new CollectionJSDataverseRepository()
const collectionExpected: Collection = {
Expand Down Expand Up @@ -45,14 +40,6 @@ describe('Collection JSDataverse Repository', () => {
it('gets the collection by id', async () => {
const collectionResponse = await CollectionHelper.create('new-collection')

// Change the api config to use bearer token
ApiConfig.init(
`${DATAVERSE_BACKEND_URL}/api/v1`,
DataverseApiAuthMechanism.BEARER_TOKEN,
undefined,
`${OIDC_AUTH_CONFIG.LOCAL_STORAGE_KEY_PREFIX}token`
)

await collectionRepository.getById(collectionResponse.id).then((collection) => {
if (!collection) {
throw new Error('Collection not found')
Expand All @@ -66,14 +53,6 @@ describe('Collection JSDataverse Repository', () => {
const uniqueCollectionId = `test-publish-collection-${timestamp}`
const collectionResponse = await CollectionHelper.create(uniqueCollectionId)

// Change the api config to use bearer token
ApiConfig.init(
`${DATAVERSE_BACKEND_URL}/api/v1`,
DataverseApiAuthMechanism.BEARER_TOKEN,
undefined,
`${OIDC_AUTH_CONFIG.LOCAL_STORAGE_KEY_PREFIX}token`
)

await collectionRepository.publish(collectionResponse.id)
await collectionRepository.getById(collectionResponse.id).then((collection) => {
if (!collection) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,6 @@ import { DatasetDTO } from '../../../../src/dataset/domain/useCases/DTOs/Dataset
import { CollectionHelper } from '../../shared/collection/CollectionHelper'
const DRAFT_PARAM = DatasetNonNumericVersion.DRAFT
import { VersionUpdateType } from '../../../../src/dataset/domain/models/VersionUpdateType'
import { ApiConfig } from '@iqss/dataverse-client-javascript'
import { DATAVERSE_BACKEND_URL, OIDC_AUTH_CONFIG } from '@/config'
import { DataverseApiAuthMechanism } from '@iqss/dataverse-client-javascript/dist/core/infra/repositories/ApiConfig'

chai.use(chaiAsPromised)
const expect = chai.expect
Expand Down Expand Up @@ -153,15 +150,6 @@ describe('Dataset JSDataverse Repository', () => {
it('gets the dataset by persistentId', async () => {
const datasetResponse = await DatasetHelper.create(collectionId)

// Change the api config to use bearer token

ApiConfig.init(
`${DATAVERSE_BACKEND_URL}/api/v1`,
DataverseApiAuthMechanism.BEARER_TOKEN,
undefined,
`${OIDC_AUTH_CONFIG.LOCAL_STORAGE_KEY_PREFIX}token`
)

await datasetRepository
.getByPersistentId(datasetResponse.persistentId, DRAFT_PARAM)
.then((dataset) => {
Expand Down Expand Up @@ -193,14 +181,6 @@ describe('Dataset JSDataverse Repository', () => {
cy.clearAllLocalStorage()
cy.clearAllCookies()

// Change the api config to use bearer token
ApiConfig.init(
`${DATAVERSE_BACKEND_URL}/api/v1`,
DataverseApiAuthMechanism.BEARER_TOKEN,
undefined,
`${OIDC_AUTH_CONFIG.LOCAL_STORAGE_KEY_PREFIX}token`
)

await datasetRepository
.getByPersistentId(datasetResponse.persistentId, '1.0')
.then((dataset) => {
Expand Down Expand Up @@ -245,15 +225,6 @@ describe('Dataset JSDataverse Repository', () => {
await DatasetHelper.publish(datasetResponse.persistentId)
await TestsUtils.waitForNoLocks(datasetResponse.persistentId)

// Change the api config to use bearer token

ApiConfig.init(
`${DATAVERSE_BACKEND_URL}/api/v1`,
DataverseApiAuthMechanism.BEARER_TOKEN,
undefined,
`${OIDC_AUTH_CONFIG.LOCAL_STORAGE_KEY_PREFIX}token`
)

await datasetRepository
.getByPersistentId(datasetResponse.persistentId, '1.0')
.then((dataset) => {
Expand Down Expand Up @@ -290,15 +261,6 @@ describe('Dataset JSDataverse Repository', () => {
it('gets the dataset by persistentId and version DRAFT keyword', async () => {
const datasetResponse = await DatasetHelper.create(collectionId)

// Change the api config to use bearer token

ApiConfig.init(
`${DATAVERSE_BACKEND_URL}/api/v1`,
DataverseApiAuthMechanism.BEARER_TOKEN,
undefined,
`${OIDC_AUTH_CONFIG.LOCAL_STORAGE_KEY_PREFIX}token`
)

await datasetRepository
.getByPersistentId(datasetResponse.persistentId, DRAFT_PARAM)
.then((dataset) => {
Expand All @@ -316,15 +278,6 @@ describe('Dataset JSDataverse Repository', () => {
const datasetResponse = await DatasetHelper.create(collectionId)
const privateUrlResponse = await DatasetHelper.createPrivateUrl(datasetResponse.id)

// Change the api config to use bearer token

ApiConfig.init(
`${DATAVERSE_BACKEND_URL}/api/v1`,
DataverseApiAuthMechanism.BEARER_TOKEN,
undefined,
`${OIDC_AUTH_CONFIG.LOCAL_STORAGE_KEY_PREFIX}token`
)

await datasetRepository.getByPrivateUrlToken(privateUrlResponse.token).then((dataset) => {
if (!dataset) {
throw new Error('Dataset not found')
Expand All @@ -345,14 +298,6 @@ describe('Dataset JSDataverse Repository', () => {

await DatasetHelper.setCitationDateFieldType(datasetResponse.persistentId, 'dateOfDeposit')

// Change the api config to use bearer token
ApiConfig.init(
`${DATAVERSE_BACKEND_URL}/api/v1`,
DataverseApiAuthMechanism.BEARER_TOKEN,
undefined,
`${OIDC_AUTH_CONFIG.LOCAL_STORAGE_KEY_PREFIX}token`
)

await datasetRepository
.getByPersistentId(datasetResponse.persistentId, '1.0')
.then((dataset) => {
Expand All @@ -374,14 +319,12 @@ describe('Dataset JSDataverse Repository', () => {
return DatasetHelper.createAndPublish(previewCollectionId).then((datasetResponse) => {
const paginationInfo = new DatasetPaginationInfo(1, 20)

// Change the api config to use bearer token

ApiConfig.init(
`${DATAVERSE_BACKEND_URL}/api/v1`,
DataverseApiAuthMechanism.BEARER_TOKEN,
undefined,
`${OIDC_AUTH_CONFIG.LOCAL_STORAGE_KEY_PREFIX}token`
)
// ApiConfig.init(
// `${DATAVERSE_BACKEND_URL}/api/v1`,
// DataverseApiAuthMechanism.BEARER_TOKEN,
// undefined,
// `${OIDC_AUTH_CONFIG.LOCAL_STORAGE_KEY_PREFIX}token`
// )

return datasetRepository
.getAllWithCount(previewCollectionId, paginationInfo)
Expand All @@ -404,15 +347,6 @@ describe('Dataset JSDataverse Repository', () => {

await DatasetHelper.deaccession(datasetResponse.id)

// Change the api config to use bearer token

ApiConfig.init(
`${DATAVERSE_BACKEND_URL}/api/v1`,
DataverseApiAuthMechanism.BEARER_TOKEN,
undefined,
`${OIDC_AUTH_CONFIG.LOCAL_STORAGE_KEY_PREFIX}token`
)

await datasetRepository.getByPersistentId(datasetResponse.persistentId).then((dataset) => {
if (!dataset) {
throw new Error('Dataset not found')
Expand All @@ -428,15 +362,6 @@ describe('Dataset JSDataverse Repository', () => {
const datasetResponse = await DatasetHelper.create(collectionId)
await DatasetHelper.lock(datasetResponse.id, DatasetLockReason.FINALIZE_PUBLICATION)

// Change the api config to use bearer token

ApiConfig.init(
`${DATAVERSE_BACKEND_URL}/api/v1`,
DataverseApiAuthMechanism.BEARER_TOKEN,
undefined,
`${OIDC_AUTH_CONFIG.LOCAL_STORAGE_KEY_PREFIX}token`
)

await datasetRepository
.getByPersistentId(datasetResponse.persistentId, DRAFT_PARAM)
.then((dataset) => {
Expand All @@ -457,15 +382,6 @@ describe('Dataset JSDataverse Repository', () => {
})

it('creates a new dataset from DatasetDTO', async () => {
// Change the api config to use bearer token

ApiConfig.init(
`${DATAVERSE_BACKEND_URL}/api/v1`,
DataverseApiAuthMechanism.BEARER_TOKEN,
undefined,
`${OIDC_AUTH_CONFIG.LOCAL_STORAGE_KEY_PREFIX}token`
)

const datasetDTO: DatasetDTO = {
metadataBlocks: [
{
Expand Down Expand Up @@ -502,15 +418,6 @@ describe('Dataset JSDataverse Repository', () => {
it('publishes a draft dataset', async () => {
const datasetResponse = await DatasetHelper.create(collectionId)

// Change the api config to use bearer token

ApiConfig.init(
`${DATAVERSE_BACKEND_URL}/api/v1`,
DataverseApiAuthMechanism.BEARER_TOKEN,
undefined,
`${OIDC_AUTH_CONFIG.LOCAL_STORAGE_KEY_PREFIX}token`
)

await datasetRepository.publish(datasetResponse.persistentId).then((response) => {
expect(response).to.not.exist
})
Expand Down
Loading

0 comments on commit 448ed61

Please sign in to comment.