Best JavaScript code snippet using ava
FormElementTest.js
Source:FormElementTest.js
...70 assertSuccess(createFormElement(dataType, mandatory).validate(5.5));71 });72 it("should return success false if passed invalid value on mandatory element", () => {73 const mandatory = true;74 assertFailure(createFormElement(dataType, mandatory).validate("abc"), "numericValueValidation");75 });76 it("should return success false if passed empty or null value on mandatory element", () => {77 const mandatory = true;78 assertFailure(createFormElement(dataType, mandatory).validate(""), "emptyValidationMessage");79 assertFailure(createFormElement(dataType, mandatory).validate(null), "emptyValidationMessage");80 });81 it("should return success false if passed value fails low or high absolute check on mandatory element", () => {82 const mandatory = true;83 assertFailure(createNumericFormElement(mandatory, 5, 5).validate(3), "numberBelowLowAbsolute");84 assertFailure(createNumericFormElement(mandatory, 5, 5).validate(6), "numberAboveHiAbsolute");85 });86 it("should return success true if passed valid value on non mandatory element", () => {87 const mandatory = false;88 assertSuccess(createFormElement(dataType, mandatory).validate(5.5));89 });90 it("should return success false if passed invalid value on non mandatory element", () => {91 const mandatory = false;92 assertFailure(createFormElement(dataType, mandatory).validate("abc"), "numericValueValidation");93 });94 it("should return success true if passed empty or null on non mandatory element", () => {95 const mandatory = false;96 assertSuccess(createFormElement(dataType, mandatory).validate(null));97 });98 });99 describe("FormElement.validate Date Concept", () => {100 let dataType = Concept.dataType.Date;101 it("should return success true if passed valid value on mandatory element", () => {102 const mandatory = true;103 assertSuccess(createFormElement(dataType, mandatory).validate("2020-02-29"));104 assertSuccess(createFormElement(dataType, mandatory).validate("2020-12-31"));105 assertSuccess(createFormElement(dataType, mandatory).validate("2020-1-1"));106 });107 it("should return success false if passed invalid value on mandatory element", () => {108 const mandatory = true;109 assertFailure(createFormElement(dataType, mandatory).validate("2020-02-30"), "invalidDateFormat");110 assertFailure(createFormElement(dataType, mandatory).validate("2019-02-29"), "invalidDateFormat");111 assertFailure(createFormElement(dataType, mandatory).validate("2019-13-01"), "invalidDateFormat");112 });113 it("should return success false if passed empty or null value on mandatory element", () => {114 const mandatory = true;115 assertFailure(createFormElement(dataType, mandatory).validate(""), "emptyValidationMessage");116 assertFailure(createFormElement(dataType, mandatory).validate(null), "emptyValidationMessage");117 });118 it("should return success true if passed valid value on non mandatory element", () => {119 const mandatory = false;120 assertSuccess(createFormElement(dataType, mandatory).validate("2020-02-29"));121 });122 it("should return success false if passed invalid value on non mandatory element", () => {123 const mandatory = false;124 assertFailure(createFormElement(dataType, mandatory).validate("2020-02-30"), "invalidDateFormat");125 assertFailure(createFormElement(dataType, mandatory).validate("2019-02-29"), "invalidDateFormat");126 assertFailure(createFormElement(dataType, mandatory).validate("2019-13-01"), "invalidDateFormat");127 });128 it("should return success true if passed empty or null on non mandatory element", () => {129 const mandatory = false;130 assertSuccess(createFormElement(dataType, mandatory).validate(null));131 assertSuccess(createFormElement(dataType, mandatory).validate(undefined));132 });133 });134 describe("FormElement.validate DateTime Concept", () => {135 let dataType = Concept.dataType.DateTime;136 it("should return success true if passed valid value on mandatory element", () => {137 const mandatory = true;138 assertSuccess(createFormElement(dataType, mandatory).validate(new Date("2020-02-29 12:00")));139 assertSuccess(createFormElement(dataType, mandatory).validate(new Date("2020-02-29 23:59")));140 });141 it("should return success false if passed invalid value on mandatory element", () => {142 const mandatory = true;143 assertFailure(createFormElement(dataType, mandatory).validate(new Date("2020-01-01 24:52")), "invalidDateTimeFormat");144 assertFailure(createFormElement(dataType, mandatory).validate(new Date("2020-01-01 12:95")), "invalidDateTimeFormat");145 assertFailure(createFormElement(dataType, mandatory).validate(new Date("2020-25-30 01:53")), "invalidDateTimeFormat");146 assertFailure(createFormElement(dataType, mandatory).validate(Date.parse("foo")), "invalidDateTimeFormat");147 assertFailure(createFormElement(dataType, mandatory).validate(new Date("2020-01-01 00:00")), "timeValueValidation");148 });149 it("should return success false if passed empty or null value on mandatory element", () => {150 const mandatory = true;151 assertFailure(createFormElement(dataType, mandatory).validate(""), "emptyValidationMessage");152 assertFailure(createFormElement(dataType, mandatory).validate(null), "emptyValidationMessage");153 });154 it("should return success true if passed valid value on non mandatory element", () => {155 const mandatory = false;156 assertSuccess(createFormElement(dataType, mandatory).validate(new Date("2020-02-29 12:00")));157 assertSuccess(createFormElement(dataType, mandatory).validate(new Date("2020-02-29 23:59")));158 });159 it("should return success false if passed invalid value on non mandatory element", () => {160 const mandatory = false;161 assertFailure(createFormElement(dataType, mandatory).validate(new Date("2020-01-01 24:52")), "invalidDateTimeFormat");162 assertFailure(createFormElement(dataType, mandatory).validate(new Date("2020-01-01 12:95")), "invalidDateTimeFormat");163 assertFailure(createFormElement(dataType, mandatory).validate(new Date("2020-25-30 01:53")), "invalidDateTimeFormat");164 assertFailure(createFormElement(dataType, mandatory).validate(Date.parse("foo")), "invalidDateTimeFormat");165 assertFailure(createFormElement(dataType, mandatory).validate(new Date("2020-01-01 00:00")), "timeValueValidation");166 });167 it("should return success true if passed empty or null on non mandatory element", () => {168 const mandatory = false;169 assertSuccess(createFormElement(dataType, mandatory).validate(null));170 });171 });172 describe("FormElement.validate Time Concept", () => {173 let dataType = Concept.dataType.Time;174 it("should return success true if passed valid value on mandatory element", () => {175 const mandatory = true;176 assertSuccess(createFormElement(dataType, mandatory).validate("23:59"));177 assertSuccess(createFormElement(dataType, mandatory).validate("0:00"));178 });179 it("should return success false if passed invalid value on mandatory element", () => {180 const mandatory = true;181 assertFailure(createFormElement(dataType, mandatory).validate("24:20"), "invalidTimeFormat");182 assertFailure(createFormElement(dataType, mandatory).validate("28:00"), "invalidTimeFormat");183 });184 it("should return success false if passed empty or null value on mandatory element", () => {185 const mandatory = true;186 assertFailure(createFormElement(dataType, mandatory).validate(""), "emptyValidationMessage");187 assertFailure(createFormElement(dataType, mandatory).validate(null), "emptyValidationMessage");188 assertFailure(createFormElement(dataType, mandatory).validate(undefined), "emptyValidationMessage");189 });190 it("should return success true if passed valid value on non mandatory element", () => {191 const mandatory = false;192 assertSuccess(createFormElement(dataType, mandatory).validate("23:59"));193 assertSuccess(createFormElement(dataType, mandatory).validate("0:00"));194 });195 it("should return success false if passed invalid value on non mandatory element", () => {196 const mandatory = false;197 assertFailure(createFormElement(dataType, mandatory).validate("24:20"), "invalidTimeFormat");198 assertFailure(createFormElement(dataType, mandatory).validate("28:00"), "invalidTimeFormat");199 });200 it("should return success true if passed empty or null value on non mandatory element", () => {201 const mandatory = false;202 assertSuccess(createFormElement(dataType, mandatory).validate(null));203 assertSuccess(createFormElement(dataType, mandatory).validate(undefined));204 });205 });206 describe("FormElement.validate Text Concept", () => {207 let dataType = Concept.dataType.Text;208 it("should return success true if passed valid value on mandatory element", () => {209 const mandatory = true;210 assertSuccess(createTextFormElement(mandatory, "^\\d{10}$", "Require 10 digits").validate("9919999199"));211 });212 it("should return success false if passed invalid value on mandatory element", () => {213 const mandatory = true;214 assertFailure(createTextFormElement(mandatory, "^\\d{10}$", "Require 10 digits").validate("991999919"), "Require 10 digits");215 });216 it("should return success false if passed empty or null value on mandatory element", () => {217 const mandatory = true;218 assertFailure(createTextFormElement(mandatory, "^\\d{10}$", "Require 10 digits").validate(""), "emptyValidationMessage");219 assertFailure(createTextFormElement(mandatory, "^\\d{10}$", "Require 10 digits").validate(null), "emptyValidationMessage");220 assertFailure(createTextFormElement(mandatory, "^\\d{10}$", "Require 10 digits").validate(undefined), "emptyValidationMessage");221 });222 it("should return success true if passed valid value on non mandatory element", () => {223 const mandatory = true;224 assertSuccess(createTextFormElement(mandatory, "^\\d{10}$", "Require 10 digits").validate("9919999199"));225 });226 it("should return success false if passed invalid value on non mandatory element", () => {227 const mandatory = true;228 assertFailure(createTextFormElement(mandatory, "^\\d{10}$", "Require 10 digits").validate("991999919"), "Require 10 digits");229 });230 it("should return success true if passed empty or null value on non mandatory element", () => {231 const mandatory = false;232 assertSuccess(createTextFormElement(mandatory, "^\\d{10}$", "Require 10 digits").validate(""));233 assertSuccess(createTextFormElement(mandatory, "^\\d{10}$", "Require 10 digits").validate(null));234 assertSuccess(createTextFormElement(mandatory, "^\\d{10}$", "Require 10 digits").validate(undefined));235 });236 });...
authentication_test.py
Source:authentication_test.py
...51 username, password, ipaddress.ip_address(ip_address))52 self.assertIsNotNone(authenticated_participation)53 self.assertIsNotNone(cookie)54 self.assertIs(authenticated_participation, self.participation)55 def assertFailure(self, username, password, ip_address):56 authenticated_participation, cookie = validate_login(57 self.session, self.contest, self.timestamp,58 username, password, ipaddress.ip_address(ip_address))59 self.assertIsNone(authenticated_participation)60 self.assertIsNone(cookie)61 def test_successful_login(self):62 self.assertSuccess("myuser", "mypass", "127.0.0.1")63 def test_no_user(self):64 self.assertFailure("myotheruser", "mypass", "127.0.0.1")65 def test_no_participation_for_user_in_contest(self):66 other_contest = self.add_contest(allow_password_authentication=True)67 other_user = self.add_user(68 username="myotheruser", password=build_password("mypass"))69 self.add_participation(contest=other_contest, user=other_user)70 self.assertFailure("myotheruser", "mypass", "127.0.0.1")71 def test_participation_specific_password(self):72 self.participation.password = build_password("myotherpass")73 self.assertFailure("myuser", "mypass", "127.0.0.1")74 self.assertSuccess("myuser", "myotherpass", "127.0.0.1")75 def test_unallowed_password_authentication(self):76 self.contest.allow_password_authentication = False77 self.assertFailure("myuser", "mypass", "127.0.0.1")78 def test_unallowed_hidden_participation(self):79 self.contest.block_hidden_participations = True80 self.participation.hidden = True81 self.assertFailure("myuser", "mypass", "127.0.0.1")82 def test_invalid_password_stored_in_user(self):83 # It's invalid, as it's not created by build_password.84 self.user.password = "mypass"85 # Mainly checks that no exception is raised.86 self.assertFailure("myuser", "mypass", "127.0.0.1")87 def test_invalid_password_stored_in_participation(self):88 # It's invalid, as it's not created by build_password.89 self.participation.password = "myotherpass"90 # Mainly checks that no exception is raised.91 self.assertFailure("myuser", "myotherpass", "127.0.0.1")92 def test_ip_lock(self):93 self.contest.ip_restriction = True94 self.participation.ip = [ipaddress.ip_network("10.0.0.0/24")]95 self.assertSuccess("myuser", "mypass", "10.0.0.1")96 self.assertFailure("myuser", "wrongpass", "10.0.0.1")97 self.assertFailure("myuser", "mypass", "10.0.1.1")98 self.participation.ip = [ipaddress.ip_network("10.9.0.0/24"),99 ipaddress.ip_network("127.0.0.1/32")]100 self.assertSuccess("myuser", "mypass", "127.0.0.1")101 self.assertFailure("myuser", "mypass", "127.0.0.0")102 self.assertSuccess("myuser", "mypass", "10.9.0.7")103 # Corner cases.104 self.participation.ip = []105 self.assertFailure("myuser", "mypass", "10.0.0.1")106 self.participation.ip = None107 self.assertSuccess("myuser", "mypass", "10.0.0.1")108 def test_deactivated_ip_lock(self):109 self.contest.ip_restriction = False110 self.participation.ip = [ipaddress.ip_network("10.0.0.0/24")]111 self.assertSuccess("myuser", "mypass", "10.0.1.1")112class TestAuthenticateRequest(DatabaseMixin, unittest.TestCase):113 def setUp(self):114 super(TestAuthenticateRequest, self).setUp()115 self.timestamp = make_datetime()116 self.contest = self.add_contest()117 self.user = self.add_user(118 username="myuser", password=build_password("mypass"))119 self.participation = self.add_participation(120 contest=self.contest, user=self.user)121 _, self.cookie = validate_login(122 self.session, self.contest, self.timestamp, self.user.username,123 "mypass", ipaddress.ip_address("10.0.0.1"))124 def attempt_authentication(self, **kwargs):125 # The arguments need to be passed as keywords and are timestamp, cookie126 # and ip_address. A missing argument means the default value is used127 # instead. An argument passed as None means that None will be used.128 return authenticate_request(129 self.session, self.contest,130 kwargs.get("timestamp", self.timestamp),131 kwargs.get("cookie", self.cookie),132 ipaddress.ip_address(kwargs.get("ip_address", "10.0.0.1")))133 def assertSuccess(self, **kwargs):134 # Assert that the authentication succeeds in any way (be it through IP135 # autologin or thanks to the cookie) and return the cookie that should136 # be set (or None, if it should be cleared/left unset).137 # The arguments are the same as those of attempt_authentication.138 authenticated_participation, cookie = \139 self.attempt_authentication(**kwargs)140 self.assertIsNotNone(authenticated_participation)141 self.assertIs(authenticated_participation, self.participation)142 return cookie143 def assertSuccessAndCookieRefreshed(self, **kwargs):144 # Assert that the authentication succeeds and that a cookie is returned145 # as well, to be refreshed on the client. (This typically indicates146 # that the authentication was performed through the cookie.)147 # The arguments are the same as those of attempt_authentication.148 cookie = self.assertSuccess(**kwargs)149 self.assertIsNotNone(cookie)150 return cookie151 def assertSuccessAndCookieCleared(self, **kwargs):152 # Assert that the authentication succeeds and no cookie is returned,153 # meaning that it needs to be cleared (or left unset) on the client.154 # (This typically indicates that the authentication occurred by IP155 # autologin.)156 # The arguments are the same as those of attempt_authentication.157 cookie = self.assertSuccess(**kwargs)158 self.assertIsNone(cookie)159 def assertFailure(self, **kwargs):160 # Assert that the authentication fails.161 # The arguments are the same as those of attempt_authentication.162 authenticated_participation, cookie = \163 self.attempt_authentication(**kwargs)164 self.assertIsNone(authenticated_participation)165 self.assertIsNone(cookie)166 @patch.object(config, "cookie_duration", 10)167 def test_cookie_contains_timestamp(self):168 self.contest.ip_autologin = False169 self.contest.allow_password_authentication = True170 # The cookie allows to authenticate.171 self.assertSuccessAndCookieRefreshed()172 # Until the duration expires.173 new_cookie = self.assertSuccessAndCookieRefreshed(174 timestamp=self.timestamp + timedelta(seconds=8))175 # But not after it expires.176 self.assertFailure(timestamp=self.timestamp + timedelta(seconds=14))177 # Unless the cookie is refreshed.178 self.assertSuccessAndCookieRefreshed(179 timestamp=self.timestamp + timedelta(seconds=14),180 cookie=new_cookie)181 def test_cookie_contains_password(self):182 self.contest.ip_autologin = False183 # Cookies are of no use if one cannot login by password.184 self.contest.allow_password_authentication = False185 self.assertFailure()186 # The cookie works with all methods as it holds the plaintext password.187 self.contest.allow_password_authentication = True188 self.user.password = hash_password("mypass", method="bcrypt")189 self.assertSuccessAndCookieRefreshed()190 self.user.password = hash_password("mypass", method="plaintext")191 self.assertSuccessAndCookieRefreshed()192 # Cookies contain the password, which is validated every time.193 self.user.password = build_password("newpass")194 self.assertFailure()195 # Contest-specific passwords take precedence over global ones.196 self.participation.password = build_password("mypass")197 self.assertSuccessAndCookieRefreshed()198 # And they do so in the negative case too.199 self.user.password = build_password("mypass")200 self.participation.password = build_password("newpass")201 self.assertFailure()202 def test_ip_autologin(self):203 self.contest.ip_autologin = True204 self.contest.allow_password_authentication = False205 self.participation.ip = [ipaddress.ip_network("10.0.0.1/32")]206 self.assertSuccessAndCookieCleared()207 self.assertFailure(ip_address="10.1.0.1")208 self.participation.ip = [ipaddress.ip_network("10.0.0.0/24")]209 self.assertFailure()210 def test_ip_autologin_with_ambiguous_addresses(self):211 # If two users have the same IP address neither of them can autologin.212 self.contest.ip_autologin = True213 self.contest.allow_password_authentication = False214 self.participation.ip = [ipaddress.ip_network("10.0.0.1/32")]215 other_user = self.add_user()216 other_participation = self.add_participation(217 contest=self.contest, user=other_user,218 ip=[ipaddress.ip_network("10.0.0.1/32")])219 self.assertFailure()220 # In fact, they don't even fall back to cookie-based authentication.221 self.contest.allow_password_authentication = True222 self.assertFailure()223 # But if IP autologin is disabled altogether, ambiguous IP addresses224 # are disregarded and cookie-based authentication kicks in.225 self.contest.ip_autologin = False226 self.assertSuccessAndCookieRefreshed()227 # Ambiguous IP addresses are allowed if only one of them is non-hidden228 # (and hidden users are barred from logging in).229 self.contest.ip_autologin = True230 self.contest.block_hidden_participations = True231 other_participation.hidden = True232 self.assertSuccessAndCookieCleared()233 # But not if hidden users aren't blocked.234 self.contest.block_hidden_participations = False235 self.assertFailure()236 def test_invalid_password_in_database(self):237 self.contest.ip_autologin = False238 self.contest.allow_password_authentication = True239 self.user.password = "not a valid password"240 self.assertFailure()241 self.user.password = build_password("mypass")242 self.participation.password = "not a valid password"243 self.assertFailure()244 def test_invalid_cookie(self):245 self.contest.ip_autologin = False246 self.contest.allow_password_authentication = True247 self.assertFailure(cookie=None)248 self.assertFailure(cookie="not a valid cookie")249 def test_no_user(self):250 self.session.delete(self.user)251 self.assertFailure()252 def test_no_participation_for_user_in_contest(self):253 self.session.delete(self.participation)254 self.assertFailure()255 def test_hidden_user(self):256 self.contest.ip_autologin = True257 self.contest.allow_password_authentication = True258 self.contest.block_hidden_participations = True259 self.participation.hidden = True260 self.assertFailure()261 def test_ip_lock(self):262 self.contest.ip_autologin = True263 self.contest.allow_password_authentication = True264 self.contest.ip_restriction = True265 self.participation.ip = [ipaddress.ip_network("10.0.0.0/24"),266 ipaddress.ip_network("127.0.0.1/32")]267 self.assertSuccessAndCookieCleared(ip_address="127.0.0.1")268 self.assertSuccessAndCookieRefreshed(ip_address="10.0.0.1")269 self.assertFailure(ip_address="10.1.0.1")270 self.contest.ip_restriction = False271 self.assertSuccessAndCookieRefreshed()272 # Corner cases.273 self.contest.ip_restriction = True274 self.participation.ip = []275 self.assertFailure()276 self.participation.ip = None277 self.assertSuccessAndCookieRefreshed()278if __name__ == "__main__":...
auth-counters.js
Source:auth-counters.js
...41 ++expected[mech].received;42 ++expected[mech].successful;43 assertStats();44}45function assertFailure(creds, mech) {46 if (expected[mech] === undefined) {47 expected[mech] = {received: 0, successful: 0};48 }49 assert.eq(test.auth(creds), false);50 ++expected[mech].received;51 assertStats();52}53// Initial condition, one auth by admin during user setups above.54// Using negotiated SCRAM-SHA-256 only.55assertStats();56// user1 should negotiate and succeed at SHA157assertSuccess({user: 'user1', pwd: 'pwd'}, 'SCRAM-SHA-1');58// user and user256 should both negotiate and success at SHA25659assertSuccess({user: 'user256', pwd: 'pwd'}, 'SCRAM-SHA-256');60assertSuccess({user: 'user', pwd: 'pwd'}, 'SCRAM-SHA-256');61// user, user1, and user256 as above, but explicitly asking for mechanisms.62assertSuccess({user: 'user1', pwd: 'pwd', mechanism: 'SCRAM-SHA-1'}, 'SCRAM-SHA-1');63assertSuccess({user: 'user256', pwd: 'pwd', mechanism: 'SCRAM-SHA-256'}, 'SCRAM-SHA-256');64assertSuccess({user: 'user', pwd: 'pwd', mechanism: 'SCRAM-SHA-1'}, 'SCRAM-SHA-1');65assertSuccess({user: 'user', pwd: 'pwd', mechanism: 'SCRAM-SHA-256'}, 'SCRAM-SHA-256');66// Incorrect password.67assertFailure({user: 'user1', pwd: 'haxx'}, 'SCRAM-SHA-1');68assertFailure({user: 'user256', pwd: 'haxx'}, 'SCRAM-SHA-256');69assertFailure({user: 'user', pwd: 'haxx'}, 'SCRAM-SHA-256');70assertFailure({user: 'user', pwd: 'haxx', mechanism: 'SCRAM-SHA-1'}, 'SCRAM-SHA-1');71// Incorrect mechanism.72assertFailure({user: 'user1', pwd: 'pwd', mechanism: 'SCRAM-SHA-256'}, 'SCRAM-SHA-256');73assertFailure({user: 'user256', pwd: 'pwd', mechanism: 'SCRAM-SHA-1'}, 'SCRAM-SHA-1');74const finalStats =75 assert.commandWorked(admin.runCommand({serverStatus: 1})).security.authentication.mechanisms;76MongoRunner.stopMongod(mongod);77printjson(finalStats);...
shouldFail.test.js
Source:shouldFail.test.js
...17 this.failer = await Failer.new();18 });19 describe('shouldFail', function () {20 it('rejects if no failure occurs', async function () {21 await assertFailure(shouldFail(this.failer.dontFail()));22 });23 it('accepts a revert', async function () {24 await shouldFail(this.failer.failWithRevert());25 });26 it('accepts a throw', async function () {27 await shouldFail(this.failer.failWithThrow());28 });29 it('accepts an out of gas', async function () {30 await shouldFail(this.failer.failWithOutOfGas({ gas: 2000000 }));31 });32 });33 describe('reverting', function () {34 it('rejects if no failure occurs', async function () {35 await assertFailure(shouldFail.reverting(this.failer.dontFail()));36 });37 it('accepts a revert', async function () {38 await shouldFail.reverting(this.failer.failWithRevert());39 });40 it('rejects a throw', async function () {41 await assertFailure(shouldFail.reverting(this.failer.failWithThrow()));42 });43 it('rejects an outOfGas', async function () {44 await assertFailure(shouldFail.reverting(this.failer.failWithOutOfGas({ gas: 2000000 })));45 });46 });47 describe('throwing', function () {48 it('rejects if no failure occurs', async function () {49 await assertFailure(shouldFail.throwing(this.failer.dontFail()));50 });51 it('accepts a throw', async function () {52 await shouldFail.throwing(this.failer.failWithThrow());53 });54 it('rejects a throw', async function () {55 await assertFailure(shouldFail.throwing(this.failer.failWithRevert()));56 });57 it('rejects an outOfGas', async function () {58 await assertFailure(shouldFail.throwing(this.failer.failWithOutOfGas({ gas: 2000000 })));59 });60 });61 describe('outOfGas', function () {62 it('rejects if no failure occurs', async function () {63 await assertFailure(shouldFail.outOfGas(this.failer.dontFail()));64 });65 it('accepts an out of gas', async function () {66 await shouldFail.outOfGas(this.failer.failWithOutOfGas({ gas: 2000000 }));67 });68 it('rejects a revert', async function () {69 await assertFailure(shouldFail.outOfGas(this.failer.failWithRevert()));70 });71 it('rejects a throw', async function () {72 await assertFailure(shouldFail.outOfGas(this.failer.failWithThrow()));73 });74 });...
Using AI Code Generation
1const test = require('ava');2const assert = require('assert');3test('foo', t => {4 t.throws(() => {5 assert.fail();6 });7});8test('bar', async t => {9 const bar = Promise.resolve('bar');10 t.is(await bar, 'bar');11});12const test = require('ava');13const assert = require('assert');14test('foo', t => {15 t.throws(() => {16 assert.fail();17 });18});19test('bar', async t => {20 const bar = Promise.resolve('bar');21 t.is(await bar, 'bar');22});
Using AI Code Generation
1const test = require('ava');2test('foo', t => {3 t.pass();4});5test('bar', async t => {6 const bar = Promise.resolve('bar');7 t.is(await bar, 'bar');8});9const test = require('ava');10test('foo', t => {11 t.pass();12});13test('bar', async t => {14 const bar = Promise.resolve('bar');15 t.is(await bar, 'bar');16});17test('baz', t => {18 t.fail();19});
Using AI Code Generation
1const test = require('ava');2const assertFailure = require('./assertFailure');3const assert = require('assert');4test('assertFailure', async t => {5 await assertFailure(async () => {6 assert(false);7 }, 'Expected assertion to fail');8});
Using AI Code Generation
1const test = require('ava');2test('will fail', t => {3 t.fail();4});5test('will pass', t => {6 t.pass();7});8const test = require('ava');9test('will throw', t => {10 t.throws(() => {11 throw new Error('foo');12 }, 'foo');13});14test('will not throw', t => {15 t.notThrows(() => {16 throw new Error('foo');17 }, 'bar');18});19const test = require('ava');20test('will throw', async t => {21 await t.throwsAsync(Promise.reject(new Error('foo')), 'foo');22});23test('will not throw', async t => {24 await t.notThrowsAsync(Promise.reject(new Error('foo')), 'bar');25});26const test = require('ava');27test('will pass', t => {28 t.pass();29});30const test = require('ava');31test('will fail', t => {32 t.fail();33});34const test = require('ava');35test('will pass', t => {36 t.regex('unicorn', /unicorn/);37});38const test = require('ava');39test('will pass', t => {40 t.notRegex('unicorn', /rainbow/);41});42const test = require('ava');43test('will pass', t => {44 t.snapshot('unicorn');45});46const test = require('ava');47test('will pass', t => {48 t.match('unicorn', 'unicorn');49});50const test = require('ava');51test('will pass', t => {52 t.notMatch('unicorn', 'rainbow');53});54const test = require('ava');
Using AI Code Generation
1const test = require('ava');2const assertFailure = require('./assertFailure');3test('assertFailure', t => {4 return assertFailure(t, Promise.reject(new Error('foo')), 'foo');5});6const test = require('ava');7const assertFailure = require('./assertFailure');8test('assertFailure', t => {9 return assertFailure(t, Promise.reject(new Error('foo')), 'bar');10});11### 10. `assertRejects(t, promise, expected)`12const test = require('ava');13const assertRejects = require('assert-rejects');14test('assertRejects', t => {15 return assertRejects(t, Promise.reject(new Error('foo')), 'foo');16});17const test = require('ava');18const assertRejects = require('assert-rejects');19test('assertRejects', t => {20 return assertRejects(t, Promise.reject(new Error('foo')), 'bar');21});22### 11. `assertThrowsAsync(t, function, expected, message)`23const test = require('ava');24const assertThrowsAsync = require('assert-throws-async');25test('assertThrowsAsync', t => {26 return assertThrowsAsync(t, Promise.reject(new Error('foo')), 'foo');27});28const test = require('ava');29const assertThrowsAsync = require('assert-throws-async');30test('assertThrowsAsync', t => {31 return assertThrowsAsync(t, Promise.reject(new Error('foo')), 'bar');32});33### 12. `assertThrowsAsyncWithMessage(t
Using AI Code Generation
1const test = require('ava');2const assert = require('assert');3test('assertFailure', t => {4 assert(false);5});6const test = require('ava');7const assert = require('assert');8test('assertFailure', t => {9 assert(true);10});11const test = require('ava');12const assert = require('assert');13test('assertFailure', t => {14 assert.equal(1, 2);15});16const test = require('ava');17const assert = require('assert');18test('assertFailure', t => {19 assert.equal(1, 1);20});21const test = require('ava');22const assert = require('assert');23test('assertFailure', t => {24 assert.equal(1, '1');25});26const test = require('ava');27const assert = require('assert');28test('assertFailure', t => {29 assert.equal(1, 1, 'one equals one');30});31const test = require('ava');32const assert = require('assert');33test('assertFailure', t => {34 assert.equal(1, 2, 'one equals one');35});36const test = require('ava');37const assert = require('assert');38test('assertFailure', t => {39 assert.equal(1, '1', 'one equals one');40});41const test = require('ava');42const assert = require('assert');43test('assertFailure', t => {44 assert.fail('a', 'b', 'one equals one');45});46const test = require('ava');47const assert = require('assert');48test('assertFailure', t
Using AI Code Generation
1const test = require('ava');2const { assertFailure, assertSuccess } = require('folktale-validations');3const { create } = require('../src/validate');4test('validate', t => {5 const validate = create({6 });7 const result = validate({ name: 'J', age: 20 });8 assertFailure(result, failures => {9 t.is(failures.length, 2);10 t.is(failures[0].message, 'Name must be longer than 3 characters');11 t.is(failures[1].message, 'You must be older than 21');12 });13});
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!