-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
120 additions
and
171 deletions.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
import { InteractionType } from '../../../src/models/objects/interactions' | ||
import { UserWithOrganization } from '../../../src/models/users/user' | ||
import { PublishedObject } from '../../../src/repositories/objects/publishedObjects' | ||
import { AuthManager } from '../../../src/services/auth' | ||
import { ObjectUseCases, SubscriptionsUseCases } from '../../../src/useCases' | ||
import { asyncIterableToPromiseOfArray } from '../../../src/utils/async' | ||
import { dbMigration } from '../../utils/dbMigrate' | ||
import { createMockUser } from '../../utils/mocks' | ||
import { uploadFile } from '../../utils/uploads' | ||
import { jest } from '@jest/globals' | ||
|
||
describe('Public URL', () => { | ||
let user: UserWithOrganization | ||
let fileCid: string | ||
let publishedObject: PublishedObject | ||
const content = 'test' | ||
|
||
beforeAll(async () => { | ||
await dbMigration.up() | ||
user = createMockUser() | ||
fileCid = await uploadFile(user, 'test.txt', content, 'text/plain') | ||
}) | ||
|
||
afterAll(async () => { | ||
await dbMigration.down() | ||
}) | ||
|
||
it('should be able to publish and retrieve', async () => { | ||
publishedObject = await ObjectUseCases.publishObject(user, fileCid) | ||
|
||
expect(publishedObject).toMatchObject({ | ||
publicId: user.publicId, | ||
cid: fileCid, | ||
id: expect.any(String), | ||
}) | ||
}) | ||
|
||
it('should be downloadable by public id and credits should be deducted', async () => { | ||
jest.spyOn(AuthManager, 'getUserFromPublicId').mockResolvedValue(user) | ||
|
||
const { metadata, startDownload } = | ||
await ObjectUseCases.downloadPublishedObject(publishedObject.id) | ||
|
||
expect(metadata).toMatchObject({ | ||
type: 'file', | ||
dataCid: publishedObject.cid, | ||
}) | ||
|
||
const pendingCredits = | ||
await SubscriptionsUseCases.getPendingCreditsByUserAndType( | ||
user, | ||
InteractionType.Download, | ||
) | ||
|
||
const downloadedContent = Buffer.concat( | ||
await asyncIterableToPromiseOfArray(await startDownload()), | ||
) | ||
expect(downloadedContent).toEqual(Buffer.from(content)) | ||
|
||
const updatedPendingCredits = | ||
await SubscriptionsUseCases.getPendingCreditsByUserAndType( | ||
user, | ||
InteractionType.Download, | ||
) | ||
|
||
expect(updatedPendingCredits).toBe( | ||
pendingCredits - downloadedContent.length, | ||
) | ||
}) | ||
}) |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,53 +1,57 @@ | ||
'use strict'; | ||
'use strict' | ||
|
||
var dbm; | ||
var type; | ||
var seed; | ||
var fs = require('fs'); | ||
var path = require('path'); | ||
var Promise; | ||
var dbm | ||
var type | ||
var seed | ||
var fs = require('fs') | ||
var path = require('path') | ||
var Promise | ||
|
||
/** | ||
* We receive the dbmigrate dependency from dbmigrate initially. | ||
* This enables us to not have to rely on NODE_PATH. | ||
*/ | ||
exports.setup = function(options, seedLink) { | ||
dbm = options.dbmigrate; | ||
type = dbm.dataType; | ||
seed = seedLink; | ||
Promise = options.Promise; | ||
}; | ||
* We receive the dbmigrate dependency from dbmigrate initially. | ||
* This enables us to not have to rely on NODE_PATH. | ||
*/ | ||
exports.setup = function (options, seedLink) { | ||
dbm = options.dbmigrate | ||
type = dbm.dataType | ||
seed = seedLink | ||
Promise = options.Promise | ||
} | ||
|
||
exports.up = function(db) { | ||
var filePath = path.join(__dirname, 'sqls', '20250130141940-add-public-urls-up.sql'); | ||
return new Promise( function( resolve, reject ) { | ||
fs.readFile(filePath, {encoding: 'utf-8'}, function(err,data){ | ||
if (err) return reject(err); | ||
console.log('received data: ' + data); | ||
exports.up = function (db) { | ||
var filePath = path.join( | ||
__dirname, | ||
'sqls', | ||
'20250130141940-add-public-urls-up.sql', | ||
) | ||
return new Promise(function (resolve, reject) { | ||
fs.readFile(filePath, { encoding: 'utf-8' }, function (err, data) { | ||
if (err) return reject(err) | ||
|
||
resolve(data); | ||
}); | ||
resolve(data) | ||
}) | ||
}).then(function (data) { | ||
return db.runSql(data) | ||
}) | ||
.then(function(data) { | ||
return db.runSql(data); | ||
}); | ||
}; | ||
} | ||
|
||
exports.down = function(db) { | ||
var filePath = path.join(__dirname, 'sqls', '20250130141940-add-public-urls-down.sql'); | ||
return new Promise( function( resolve, reject ) { | ||
fs.readFile(filePath, {encoding: 'utf-8'}, function(err,data){ | ||
if (err) return reject(err); | ||
console.log('received data: ' + data); | ||
exports.down = function (db) { | ||
var filePath = path.join( | ||
__dirname, | ||
'sqls', | ||
'20250130141940-add-public-urls-down.sql', | ||
) | ||
return new Promise(function (resolve, reject) { | ||
fs.readFile(filePath, { encoding: 'utf-8' }, function (err, data) { | ||
if (err) return reject(err) | ||
|
||
resolve(data); | ||
}); | ||
resolve(data) | ||
}) | ||
}).then(function (data) { | ||
return db.runSql(data) | ||
}) | ||
.then(function(data) { | ||
return db.runSql(data); | ||
}); | ||
}; | ||
} | ||
|
||
exports._meta = { | ||
"version": 1 | ||
}; | ||
version: 1, | ||
} |
7 changes: 3 additions & 4 deletions
7
backend/migrations/sqls/20250130141940-add-public-urls-up.sql
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,10 @@ | ||
CREATE TABLE published_objects ( | ||
id UUID PRIMARY KEY, | ||
public_id VARCHAR(255) NOT NULL, | ||
cid VARCHAR(255) NOT NULL, | ||
id text PRIMARY KEY, | ||
public_id text NOT NULL, | ||
cid text NOT NULL, | ||
created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP, | ||
updated_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP | ||
); | ||
|
||
CREATE INDEX idx_published_objects_public_id ON published_objects(public_id); | ||
CREATE INDEX idx_published_objects_cid ON published_objects(cid); | ||
` |
This file contains 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
This file was deleted.
Oops, something went wrong.