Skip to content

Commit

Permalink
Тесты и публикация в npmjs
Browse files Browse the repository at this point in the history
  • Loading branch information
Alex Step committed Mar 14, 2021
1 parent 49f7543 commit 25ae832
Show file tree
Hide file tree
Showing 6 changed files with 110 additions and 6 deletions.
35 changes: 35 additions & 0 deletions .github/workflows/npmpublish.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
name: Check and Publish Package

on:
pull_request:
branches:
- main
push:
branches:
- main

jobs:
test:
runs-on: ubuntu-latest
env:
USERPASS: ${{secrets.USERPASS}}
steps:
- uses: actions/checkout@v1
- uses: actions/setup-node@v1
with:
node-version: 12
- run: npm ci
- run: npm test

publish-npm:
needs: test
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v1
- uses: actions/setup-node@v1
with:
node-version: 12
registry-url: https://registry.npmjs.org/
- run: npm publish
env:
NODE_AUTH_TOKEN: ${{secrets.npm_token}}
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@

# Logs
logs
*.log
Expand Down
18 changes: 16 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,16 @@
# moy-nalog
Обёртка для API сервиса lknpd.nalog.ru
# Отправка чеков в налоговую
Неофициальная обёртка для API сервиса lknpd.nalog.ru

Пригодится для автоматизации отправки информации о доходах самозанятых.


## Использование

...


[Подробное описание методов класса](/blob/main/docs/nalogAPIClass.md)


## Разработка
...
8 changes: 5 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,8 @@ class NalogAPI {
return this.token
}

await this.authPromise

const tokenPayload = {
deviceInfo: {
appVersion: '1.0.0',
Expand All @@ -118,7 +120,7 @@ class NalogAPI {
},
refreshToken: this.refreshToken
}
await this.authPromise

const response = await fetch(this.apiUrl + '/auth/token', {
method: 'POST',
headers: {
Expand Down Expand Up @@ -165,7 +167,7 @@ class NalogAPI {
}

if (method === 'GET') delete params.body
await this.authPromise

return fetch(this.apiUrl + '/' + endpoint, params).then(r => r.json())
}

Expand Down Expand Up @@ -198,7 +200,7 @@ class NalogAPI {

services: [{
name: name, // 'Предоставление информационных услуг #970/2495',
amount: Number(amount),
amount: Number(amount.toFixed(2)),
quantity: Number(quantity)
}],

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"main": "index.js",
"scripts": {
"docs": "npx jsdoc-to-markdown ./index.js > docs/nalogAPIClass.md",
"test": "echo \"Error: no test specified\" && exit 1"
"test": "node tests/index.test.js"
},
"repository": {
"type": "git",
Expand Down
52 changes: 52 additions & 0 deletions tests/index.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
const NalogAPI = require('../index.js')

if (!process.env.USERPASS) {
process.exit(1)
}

const [USER, PASS] = process.env.USERPASS.split(':')

const tests = {
'Логин и пароль - обязательны при autologin': async () => {
try {
// eslint-disable-next-line no-unused-vars
const ok = new NalogAPI({ password: PASS })
} catch (err) {
return true
}
},

'Проверка авто-логина': async () => {
const MyNalog = new NalogAPI({ login: USER, password: PASS })
const profile = await MyNalog.userInfo()

if (!profile || !profile.id) {
console.error(profile)
throw new Error('Ошибка получения информации о пользователе')
}
return true
},

'Самостоятельный логин': async () => {
const MyNalog = new NalogAPI({ autologin: false })
if (MyNalog.authPromise) { throw new Error('сработал автологин') }
const resp = await MyNalog.auth(USER, PASS)
if (!resp || !resp.refreshToken) {
console.error(resp)
throw new Error('Ошибка логина')
}
return true
}
};

(async () => {
for (const testName in tests) {
const result = await tests[testName]()
if (result) {
console.info(`[OK] - ${testName}`)
} else {
console.error(`[FAIL] - ${testName}`, result)
process.exit(1)
}
}
})()

0 comments on commit 25ae832

Please sign in to comment.