Commit Graph

33 Commits

Author SHA1 Message Date
Naz
c8d840c211 Improved test coverage for limit-service module
no issue

- The aim is to achieve 100% unit test coverage for servies and small modules. This change covers few more bases brining limit-service's module coverage from 80% to 94%.
2021-10-25 18:18:04 +04:00
Naz
ad0d1ab557 Fixed indescribable error in allowlist limit
https://github.com/TryGhost/Team/issues/663

-  When there is no parameter passed at all it was a generic 'Cannot read property 'value' of undefined' message which wasn't helpful in recognizing what the actual problem was
- Have added additional guarding logic to throw a descriptive error
2021-05-21 14:09:27 +04:00
Naz
f0a5fc975c Fixed error "swallowing"
no issue

- I've discovered the "IncorrectUsageError" error was silently swallowed and the method returned a false positibe when an allowlist limit type was called with incorrect parameters
- In cases like this it's best to surface the real error early otherwise the logic might produce unsafe results!
2021-05-21 14:02:35 +04:00
Naz
9632e98dd2 Added a convenience method checking if any limits are acceded
refs https://github.com/TryGhost/Team/issues/662

- There is a need to check if any of the current limits are over limit in Daisy. This method is the simplest possible implementation to check if any of them are over limit
- Possible future iterations might include a list of names of the limits that have been acceded and their error messages
- The `checkIfAnyOverLimit` method should be treated as a starter to work up the complexity as needed
2021-05-21 13:24:55 +04:00
Naz
88f0c332b4 Exposed additional "name" variable in error templates
refs https://github.com/TryGhost/Team/issues/587

- There was a need to be able to use the "name" of the limit inside of error templates like so: `{{name}}` (reference https://github.com/TryGhost/Team/issues/587#issuecomment-814281794)
- This change allows to form custom error messages using following variable: `{{name}}` which is the same as the `name` property provided in the configuration for the limit
2021-05-12 14:43:52 +04:00
Naz
612cc2b513 Added addedCount to max and maxPeriodic limits
refs https://github.com/TryGhost/Team/issues/588

- The `addedCount` parameter in `errorIfWouldGoOverLimit` method allows to specify a custom resource count that is about to be added. Example usecase is when we'd want to send a 100 emails and current limit is 99, and none have been sent so far. With previous implementation the check would've passed because it only checked for single resource that would be added through "+1". Current implementation allows to specify the amount of recources to be added
2021-05-07 18:13:01 +04:00
Naz
bd360e620f Removed date-fns dev dependency
refs https://github.com/TryGhost/Team/issues/588
refs 6a1e722648

- date-fns proved to be unable to manipulate dates in consistent UTC format and was substitured with luxon in referenced commit. Removing it from tests for consistency
2021-05-07 14:56:40 +04:00
Naz
16e21236ba Clarified test name 2021-05-07 11:46:33 +04:00
Naz
9c738b13d1 Added added maxPeriodical checks
refs https://github.com/TryGhost/Team/issues/588

- This bit is putting together all the pieces for periodical limit checks. More tests are to come
2021-05-06 17:48:31 +04:00
Naz
0d242b96ef Added maxPeriodic limit support to limit service
refs https://github.com/TryGhost/Team/issues/588

- The limit service can now be initialized with a config which has a 'maxPeriodic' key identifying it's a special type of limit taking subscription cycles into account
- Example configuration can be found in the included unit tests
2021-05-06 15:50:36 +04:00
Naz
1483a5c758 Fixed IncorrectUsageError initialization
no issue

- The error takes in an options object which should contain "message" property instead of a string
2021-05-06 15:50:36 +04:00
Naz
4f41c2a206 Added utility calculating date of last period start
refs https://github.com/TryGhost/Team/issues/588

- There's a need to calculate when the last period has started to be able to generate correct counting queries for the "maxPeriodical" limit
- It operatest on ISO strings as an input and output in UTC timezone to take timezone calculations out of the equation
- Refer to inclucded unit tests for example calculations
2021-05-06 15:50:36 +04:00
Naz
413549f9c0 Added "maxPeriodic" limit type
refs https://github.com/TryGhost/Team/issues/588

- This is a scaffolding for a new limit type which should allow to check limits based on periods (for example related to billing, subscription cycles)
2021-05-06 15:50:36 +04:00
Naz
a240582094 Fixed typos 2021-05-05 12:42:30 +04:00
Naz
47a6956175 Added test coverage for flag type of limits
refs https://github.com/TryGhost/Team/issues/588

- This is by no means an thorought test coverage but ensures the basics work and provides examples of how the limit should be used. To be continued :)
2021-05-03 12:02:01 +04:00
Naz
a5eba60c23 Added flag limit support for "emails"
refs https://github.com/TryGhost/Team/issues/588

- This is a step 1 in the introduction of email limits. Next step would be allowing this limit to support "periodical limit checks"
2021-05-03 11:47:55 +04:00
Naz
1802d46c1d Added a limit reset when loadLimits called repeatedly
refs https://github.com/TryGhost/Team/issues/599

- There are cases when there'a a need to reload limits with a new set of configuration. For example, when Ghost is run in a test environment is a soft reboot is done
- Resetting previous value of limits avoids having conflicting state after multiple calls
2021-04-09 23:44:12 +12:00
Thibaut Patel
8381346dce Added allowlist limit (#144)
issue https://github.com/TryGhost/Team/issues/510
2021-04-08 17:29:53 +02:00
Thibaut Patel
8c6ec8b214 Added a test to confirm isLimited behavior of an unkown key
no issue
2021-04-08 15:07:30 +02:00
Naz
01f18aa4fa Added test coverage for {{max}} and {{count}}
refs https://github.com/TryGhost/Team/issues/510

- {{max}} and {{count}} variable usage was not covered but had valid usecases in the library client's, so considered to "document" them through tests
- For more context these variables are available in custom `error` templates that are provided with each limit
2021-04-07 13:31:42 +12:00
Naz
f4f48712c5 Added custom count queries for "max" limits
refs https://github.com/TryGhost/Team/issues/597

- When the library is used on a client without a DB connection (e.g. frontend client running in a browser) the library needs to expose a way to override count queries.
- The way these can be used is giving a count based on a HTTP request or some other data provider
- Example use with max limit like "staff" would be loading the limit servcie if following way:
```
            const limitService = new LimitService();

            let limits = {
                staff: {
                    max: 2,
                    currentCountQuery: () => 5
                }
            };

            limitService.loadLimits({limits, errors});

            await limitService.checkIsOverLimit('staff')
```
2021-04-06 16:45:46 +12:00
Naz
326cbf0d34 Fixed test missing a whitespace
refs https://github.com/TryGhost/Team/issues/597
refs a1962f38cd
2021-04-05 16:29:07 +12:00
Naz
a1962f38cd Removed ghost-ignition's errors dependency
refs https://github.com/TryGhost/Team/issues/597

- To be able to transpile the library for different runtimes (make it polymorphic) had to get rid of dependencies that were not compatible with ES  Modules
- By making errors an injectable constructor option it removes the depencency and allows to transpile the library for multiple targets
- The `errors` option is now a required parameter for `loadLimits` method. It errors if it's missing (error message copy inspired by content api error 69fcea0582/packages/content-api/lib/index.js (L21))
2021-04-05 16:17:57 +12:00
Naz
2d5aff434c Added optional max limit override to errorIfIsOverLimit
refs https://github.com/TryGhost/Team/issues/587
refs 73e7319406

- It's a symmetric change to the one introduce in the refenreced commit
- TLDR: allows to check if limit was reached if the user changes the limit
2021-04-01 18:27:29 +13:00
Naz
5dd3752f15 Added test coverage for is over limit check
refs https://github.com/TryGhost/Team/issues/587

- There was no test coverage for MaxLimit's errorIfIsOverLimit check. Added basic test to make sure upcoming modifications don't break existing functionality
2021-04-01 18:10:07 +13:00
Naz
3bcc4256f5 Grouped MaxLimit test cases into describe groups
refs https://github.com/TryGhost/Team/issues/587

- Clenup before adding even more test coverage
2021-04-01 18:03:32 +13:00
Naz
73e7319406 Added optional max limit override to errorIfWouldGoOverLimit
refs https://github.com/TryGhost/Team/issues/587

- The optional {max} passed as an option allows to override currently configured limit and do a theoretical new limit check. For example:  check if the max limit will be exceeded if the limit changes (user changes plans)
2021-04-01 17:59:52 +13:00
Naz
57e24b3677 Added incorrect 'max' usage error to MaxLimit
refs https://github.com/TryGhost/Team/issues/587

- When the 'max' configuration is missing the instance of the class breaks when used unexpectedly. Followed similar approach to currentCountQuery check  by failing fast in the constructor
2021-04-01 17:20:56 +13:00
Naz
299d3e3bd4 Added test coverage for max limit class
refs https://github.com/TryGhost/Team/issues/587

- Test were missing for class initialization and around how the limit currently works.
- Before extending it's behavior throught its valuable to cover current functionality to not accidentally break anything
2021-04-01 17:17:45 +13:00
Hannah Wolfe
2163ec6057 Changed casing of limit names + fixed handling
refs: https://github.com/TryGhost/Team/issues/510

- Ghost config always uses camelcase. This was incorrectly implemented with snake case originally
- Swap to use camelCase by default, which is desirable, but support both
- It's really easy to support both in the loader and isLimited check, so we do this to stop ourselves tripping on this later
2021-03-04 20:39:09 +00:00
Hannah Wolfe
47478eb1f9 Added proper number formatting for error messages
refs: https://github.com/TryGhost/Team/issues/510

- We should always format numbers correctly with thousand separators when we're displaying them to users
2021-03-04 18:08:25 +00:00
Hannah Wolfe
feb872eb3e Fixed clobbering the lodash template settings
refs: https://github.com/lodash/lodash/issues/705

- Was seeing unexpected token = errors when using lodash templates in Ghost
- This is because we're setting template settings globally in this dependency and it affects every other user of lodash
- Using runInContext keeps this templateSettings change local to this lib
- Test proves that after requiring limits we can require lodash and have the default values again
2021-03-03 17:54:36 +00:00
Hannah Wolfe
201e133386 Added limit service initial commit
- This provides some basic functionality and error message generation for adding host-based limits in Ghost
- It is a first-pass, needs unit tests etc
2021-03-03 12:19:48 +00:00