Software /
code /
prosody
Annotate
spec/util_error_spec.lua @ 12642:9061f9621330
Switch to a new role-based authorization framework, removing is_admin()
We began moving away from simple "is this user an admin?" permission checks
before 0.12, with the introduction of mod_authz_internal and the ability to
dynamically change the roles of individual users.
The approach in 0.12 still had various limitations however, and apart from
the introduction of roles other than "admin" and the ability to pull that info
from storage, not much actually changed.
This new framework shakes things up a lot, though aims to maintain the same
functionality and behaviour on the surface for a default Prosody
configuration. That is, if you don't take advantage of any of the new
features, you shouldn't notice any change.
The biggest change visible to developers is that usermanager.is_admin() (and
the auth provider is_admin() method) have been removed. Gone. Completely.
Permission checks should now be performed using a new module API method:
module:may(action_name, context)
This method accepts an action name, followed by either a JID (string) or
(preferably) a table containing 'origin'/'session' and 'stanza' fields (e.g.
the standard object passed to most events). It will return true if the action
should be permitted, or false/nil otherwise.
Modules should no longer perform permission checks based on the role name.
E.g. a lot of code previously checked if the user's role was prosody:admin
before permitting some action. Since many roles might now exist with similar
permissions, and the permissions of prosody:admin may be redefined
dynamically, it is no longer suitable to use this method for permission
checks. Use module:may().
If you start an action name with ':' (recommended) then the current module's
name will automatically be used as a prefix.
To define a new permission, use the new module API:
module:default_permission(role_name, action_name)
module:default_permissions(role_name, { action_name[, action_name...] })
This grants the specified role permission to execute the named action(s) by
default. This may be overridden via other mechanisms external to your module.
The built-in roles that developers should use are:
- prosody:user (normal user)
- prosody:admin (host admin)
- prosody:operator (global admin)
The new prosody:operator role is intended for server-wide actions (such as
shutting down Prosody).
Finally, all usage of is_admin() in modules has been fixed by this commit.
Some of these changes were trickier than others, but no change is expected to
break existing deployments.
EXCEPT: mod_auth_ldap no longer supports the ldap_admin_filter option. It's
very possible nobody is using this, but if someone is then we can later update
it to pull roles from LDAP somehow.
author | Matthew Wild <mwild1@gmail.com> |
---|---|
date | Wed, 15 Jun 2022 12:15:01 +0100 |
parent | 11221:b0a563716334 |
child | 13080:031382b207ec |
rev | line source |
---|---|
10101 | 1 local errors = require "util.error" |
2 | |
3 describe("util.error", function () | |
4 describe("new()", function () | |
5 it("works", function () | |
6 local err = errors.new("bork", "bork bork"); | |
7 assert.not_nil(err); | |
8 assert.equal("cancel", err.type); | |
9 assert.equal("undefined-condition", err.condition); | |
10 assert.same("bork bork", err.context); | |
11 end); | |
12 | |
13 describe("templates", function () | |
14 it("works", function () | |
15 local templates = { | |
16 ["fail"] = { | |
17 type = "wait", | |
18 condition = "internal-server-error", | |
10365
744ca71a49f7
util.error: Add well-known field 'code' in error templates
Kim Alvefur <zash@zash.se>
parents:
10101
diff
changeset
|
19 code = 555; |
10101 | 20 }; |
21 }; | |
22 local err = errors.new("fail", { traceback = "in some file, somewhere" }, templates); | |
23 assert.equal("wait", err.type); | |
24 assert.equal("internal-server-error", err.condition); | |
10365
744ca71a49f7
util.error: Add well-known field 'code' in error templates
Kim Alvefur <zash@zash.se>
parents:
10101
diff
changeset
|
25 assert.equal(555, err.code); |
10101 | 26 assert.same({ traceback = "in some file, somewhere" }, err.context); |
27 end); | |
28 end); | |
29 | |
30 end); | |
31 | |
32 describe("is_err()", function () | |
33 it("works", function () | |
34 assert.truthy(errors.is_err(errors.new())); | |
35 assert.falsy(errors.is_err("not an error")); | |
36 end); | |
37 end); | |
38 | |
39 describe("coerce", function () | |
40 it("works", function () | |
41 local ok, err = errors.coerce(nil, "it dun goofed"); | |
42 assert.is_nil(ok); | |
43 assert.truthy(errors.is_err(err)) | |
44 end); | |
45 end); | |
46 | |
47 describe("from_stanza", function () | |
48 it("works", function () | |
49 local st = require "util.stanza"; | |
50 local m = st.message({ type = "chat" }); | |
11092
bd13aa89262d
util.error: Collect Application-Specific Conditions from stanza errors
Kim Alvefur <zash@zash.se>
parents:
11089
diff
changeset
|
51 local e = st.error_reply(m, "modify", "bad-request", nil, "error.example"):tag("extra", { xmlns = "xmpp:example.test" }); |
10101 | 52 local err = errors.from_stanza(e); |
53 assert.truthy(errors.is_err(err)); | |
54 assert.equal("modify", err.type); | |
55 assert.equal("bad-request", err.condition); | |
56 assert.equal(e, err.context.stanza); | |
11089
35d2260644d9
util.error: Extract error originator from stanza errors
Kim Alvefur <zash@zash.se>
parents:
11081
diff
changeset
|
57 assert.equal("error.example", err.context.by); |
11092
bd13aa89262d
util.error: Collect Application-Specific Conditions from stanza errors
Kim Alvefur <zash@zash.se>
parents:
11089
diff
changeset
|
58 assert.not_nil(err.extra.tag); |
10101 | 59 end); |
60 end); | |
61 | |
62 describe("__tostring", function () | |
63 it("doesn't throw", function () | |
64 assert.has_no.errors(function () | |
65 -- See 6f317e51544d | |
66 tostring(errors.new()); | |
67 end); | |
68 end); | |
69 end); | |
70 | |
11081
0b68697450c5
util.error: Add well-known field 'extra'
Kim Alvefur <zash@zash.se>
parents:
10365
diff
changeset
|
71 describe("extra", function () |
0b68697450c5
util.error: Add well-known field 'extra'
Kim Alvefur <zash@zash.se>
parents:
10365
diff
changeset
|
72 it("keeps some extra fields", function () |
0b68697450c5
util.error: Add well-known field 'extra'
Kim Alvefur <zash@zash.se>
parents:
10365
diff
changeset
|
73 local err = errors.new({condition="gone",text="Sorry mate, it's all gone",extra={uri="file:///dev/null"}}); |
0b68697450c5
util.error: Add well-known field 'extra'
Kim Alvefur <zash@zash.se>
parents:
10365
diff
changeset
|
74 assert.is_table(err.extra); |
0b68697450c5
util.error: Add well-known field 'extra'
Kim Alvefur <zash@zash.se>
parents:
10365
diff
changeset
|
75 assert.equal("file:///dev/null", err.extra.uri); |
0b68697450c5
util.error: Add well-known field 'extra'
Kim Alvefur <zash@zash.se>
parents:
10365
diff
changeset
|
76 end); |
0b68697450c5
util.error: Add well-known field 'extra'
Kim Alvefur <zash@zash.se>
parents:
10365
diff
changeset
|
77 end) |
0b68697450c5
util.error: Add well-known field 'extra'
Kim Alvefur <zash@zash.se>
parents:
10365
diff
changeset
|
78 |
11097
f23cf8e2e2ff
util.error: Cover registry initialization in test
Kim Alvefur <zash@zash.se>
parents:
11092
diff
changeset
|
79 describe("init", function() |
f23cf8e2e2ff
util.error: Cover registry initialization in test
Kim Alvefur <zash@zash.se>
parents:
11092
diff
changeset
|
80 it("basics works", function() |
f23cf8e2e2ff
util.error: Cover registry initialization in test
Kim Alvefur <zash@zash.se>
parents:
11092
diff
changeset
|
81 local reg = errors.init("test", { |
f23cf8e2e2ff
util.error: Cover registry initialization in test
Kim Alvefur <zash@zash.se>
parents:
11092
diff
changeset
|
82 broke = {type = "cancel"; condition = "internal-server-error"; text = "It broke :("}; |
f23cf8e2e2ff
util.error: Cover registry initialization in test
Kim Alvefur <zash@zash.se>
parents:
11092
diff
changeset
|
83 nope = {type = "auth"; condition = "not-authorized"; text = "Can't let you do that Dave"}; |
f23cf8e2e2ff
util.error: Cover registry initialization in test
Kim Alvefur <zash@zash.se>
parents:
11092
diff
changeset
|
84 }); |
f23cf8e2e2ff
util.error: Cover registry initialization in test
Kim Alvefur <zash@zash.se>
parents:
11092
diff
changeset
|
85 |
f23cf8e2e2ff
util.error: Cover registry initialization in test
Kim Alvefur <zash@zash.se>
parents:
11092
diff
changeset
|
86 local broke = reg.new("broke"); |
f23cf8e2e2ff
util.error: Cover registry initialization in test
Kim Alvefur <zash@zash.se>
parents:
11092
diff
changeset
|
87 assert.equal("cancel", broke.type); |
f23cf8e2e2ff
util.error: Cover registry initialization in test
Kim Alvefur <zash@zash.se>
parents:
11092
diff
changeset
|
88 assert.equal("internal-server-error", broke.condition); |
f23cf8e2e2ff
util.error: Cover registry initialization in test
Kim Alvefur <zash@zash.se>
parents:
11092
diff
changeset
|
89 assert.equal("It broke :(", broke.text); |
f23cf8e2e2ff
util.error: Cover registry initialization in test
Kim Alvefur <zash@zash.se>
parents:
11092
diff
changeset
|
90 assert.equal("test", broke.source); |
f23cf8e2e2ff
util.error: Cover registry initialization in test
Kim Alvefur <zash@zash.se>
parents:
11092
diff
changeset
|
91 |
f23cf8e2e2ff
util.error: Cover registry initialization in test
Kim Alvefur <zash@zash.se>
parents:
11092
diff
changeset
|
92 local nope = reg.new("nope"); |
f23cf8e2e2ff
util.error: Cover registry initialization in test
Kim Alvefur <zash@zash.se>
parents:
11092
diff
changeset
|
93 assert.equal("auth", nope.type); |
f23cf8e2e2ff
util.error: Cover registry initialization in test
Kim Alvefur <zash@zash.se>
parents:
11092
diff
changeset
|
94 assert.equal("not-authorized", nope.condition); |
f23cf8e2e2ff
util.error: Cover registry initialization in test
Kim Alvefur <zash@zash.se>
parents:
11092
diff
changeset
|
95 assert.equal("Can't let you do that Dave", nope.text); |
f23cf8e2e2ff
util.error: Cover registry initialization in test
Kim Alvefur <zash@zash.se>
parents:
11092
diff
changeset
|
96 end); |
11100
3aa06cdd2dc8
util.error: Add a "compact mode" for registries
Kim Alvefur <zash@zash.se>
parents:
11097
diff
changeset
|
97 |
3aa06cdd2dc8
util.error: Add a "compact mode" for registries
Kim Alvefur <zash@zash.se>
parents:
11097
diff
changeset
|
98 it("compact mode works", function() |
11102
5a0ff475ecfd
util.error: Drop registry initialization with namespace as key
Kim Alvefur <zash@zash.se>
parents:
11101
diff
changeset
|
99 local reg = errors.init("test", "spec", { |
11100
3aa06cdd2dc8
util.error: Add a "compact mode" for registries
Kim Alvefur <zash@zash.se>
parents:
11097
diff
changeset
|
100 broke = {"cancel"; "internal-server-error"; "It broke :("}; |
3aa06cdd2dc8
util.error: Add a "compact mode" for registries
Kim Alvefur <zash@zash.se>
parents:
11097
diff
changeset
|
101 nope = {"auth"; "not-authorized"; "Can't let you do that Dave"; "sorry-dave"}; |
3aa06cdd2dc8
util.error: Add a "compact mode" for registries
Kim Alvefur <zash@zash.se>
parents:
11097
diff
changeset
|
102 }); |
3aa06cdd2dc8
util.error: Add a "compact mode" for registries
Kim Alvefur <zash@zash.se>
parents:
11097
diff
changeset
|
103 |
3aa06cdd2dc8
util.error: Add a "compact mode" for registries
Kim Alvefur <zash@zash.se>
parents:
11097
diff
changeset
|
104 local broke = reg.new("broke"); |
3aa06cdd2dc8
util.error: Add a "compact mode" for registries
Kim Alvefur <zash@zash.se>
parents:
11097
diff
changeset
|
105 assert.equal("cancel", broke.type); |
3aa06cdd2dc8
util.error: Add a "compact mode" for registries
Kim Alvefur <zash@zash.se>
parents:
11097
diff
changeset
|
106 assert.equal("internal-server-error", broke.condition); |
3aa06cdd2dc8
util.error: Add a "compact mode" for registries
Kim Alvefur <zash@zash.se>
parents:
11097
diff
changeset
|
107 assert.equal("It broke :(", broke.text); |
3aa06cdd2dc8
util.error: Add a "compact mode" for registries
Kim Alvefur <zash@zash.se>
parents:
11097
diff
changeset
|
108 assert.is_nil(broke.extra); |
3aa06cdd2dc8
util.error: Add a "compact mode" for registries
Kim Alvefur <zash@zash.se>
parents:
11097
diff
changeset
|
109 |
3aa06cdd2dc8
util.error: Add a "compact mode" for registries
Kim Alvefur <zash@zash.se>
parents:
11097
diff
changeset
|
110 local nope = reg.new("nope"); |
3aa06cdd2dc8
util.error: Add a "compact mode" for registries
Kim Alvefur <zash@zash.se>
parents:
11097
diff
changeset
|
111 assert.equal("auth", nope.type); |
3aa06cdd2dc8
util.error: Add a "compact mode" for registries
Kim Alvefur <zash@zash.se>
parents:
11097
diff
changeset
|
112 assert.equal("not-authorized", nope.condition); |
3aa06cdd2dc8
util.error: Add a "compact mode" for registries
Kim Alvefur <zash@zash.se>
parents:
11097
diff
changeset
|
113 assert.equal("Can't let you do that Dave", nope.text); |
3aa06cdd2dc8
util.error: Add a "compact mode" for registries
Kim Alvefur <zash@zash.se>
parents:
11097
diff
changeset
|
114 assert.equal("spec", nope.extra.namespace); |
3aa06cdd2dc8
util.error: Add a "compact mode" for registries
Kim Alvefur <zash@zash.se>
parents:
11097
diff
changeset
|
115 assert.equal("sorry-dave", nope.extra.condition); |
3aa06cdd2dc8
util.error: Add a "compact mode" for registries
Kim Alvefur <zash@zash.se>
parents:
11097
diff
changeset
|
116 end); |
11101
2288d206b14b
util.error: Expand compact registries into normal form internally
Kim Alvefur <zash@zash.se>
parents:
11100
diff
changeset
|
117 |
2288d206b14b
util.error: Expand compact registries into normal form internally
Kim Alvefur <zash@zash.se>
parents:
11100
diff
changeset
|
118 it("registry looks the same regardless of syntax", function() |
2288d206b14b
util.error: Expand compact registries into normal form internally
Kim Alvefur <zash@zash.se>
parents:
11100
diff
changeset
|
119 local normal = errors.init("test", { |
2288d206b14b
util.error: Expand compact registries into normal form internally
Kim Alvefur <zash@zash.se>
parents:
11100
diff
changeset
|
120 broke = {type = "cancel"; condition = "internal-server-error"; text = "It broke :("}; |
2288d206b14b
util.error: Expand compact registries into normal form internally
Kim Alvefur <zash@zash.se>
parents:
11100
diff
changeset
|
121 nope = { |
2288d206b14b
util.error: Expand compact registries into normal form internally
Kim Alvefur <zash@zash.se>
parents:
11100
diff
changeset
|
122 type = "auth"; |
2288d206b14b
util.error: Expand compact registries into normal form internally
Kim Alvefur <zash@zash.se>
parents:
11100
diff
changeset
|
123 condition = "not-authorized"; |
2288d206b14b
util.error: Expand compact registries into normal form internally
Kim Alvefur <zash@zash.se>
parents:
11100
diff
changeset
|
124 text = "Can't let you do that Dave"; |
2288d206b14b
util.error: Expand compact registries into normal form internally
Kim Alvefur <zash@zash.se>
parents:
11100
diff
changeset
|
125 extra = {namespace = "spec"; condition = "sorry-dave"}; |
2288d206b14b
util.error: Expand compact registries into normal form internally
Kim Alvefur <zash@zash.se>
parents:
11100
diff
changeset
|
126 }; |
2288d206b14b
util.error: Expand compact registries into normal form internally
Kim Alvefur <zash@zash.se>
parents:
11100
diff
changeset
|
127 }); |
11102
5a0ff475ecfd
util.error: Drop registry initialization with namespace as key
Kim Alvefur <zash@zash.se>
parents:
11101
diff
changeset
|
128 local compact1 = errors.init("test", "spec", { |
11101
2288d206b14b
util.error: Expand compact registries into normal form internally
Kim Alvefur <zash@zash.se>
parents:
11100
diff
changeset
|
129 broke = {"cancel"; "internal-server-error"; "It broke :("}; |
2288d206b14b
util.error: Expand compact registries into normal form internally
Kim Alvefur <zash@zash.se>
parents:
11100
diff
changeset
|
130 nope = {"auth"; "not-authorized"; "Can't let you do that Dave"; "sorry-dave"}; |
2288d206b14b
util.error: Expand compact registries into normal form internally
Kim Alvefur <zash@zash.se>
parents:
11100
diff
changeset
|
131 }); |
11102
5a0ff475ecfd
util.error: Drop registry initialization with namespace as key
Kim Alvefur <zash@zash.se>
parents:
11101
diff
changeset
|
132 local compact2 = errors.init("test", { |
11101
2288d206b14b
util.error: Expand compact registries into normal form internally
Kim Alvefur <zash@zash.se>
parents:
11100
diff
changeset
|
133 broke = {"cancel"; "internal-server-error"; "It broke :("}; |
2288d206b14b
util.error: Expand compact registries into normal form internally
Kim Alvefur <zash@zash.se>
parents:
11100
diff
changeset
|
134 nope = {"auth"; "not-authorized"; "Can't let you do that Dave"}; |
2288d206b14b
util.error: Expand compact registries into normal form internally
Kim Alvefur <zash@zash.se>
parents:
11100
diff
changeset
|
135 }); |
2288d206b14b
util.error: Expand compact registries into normal form internally
Kim Alvefur <zash@zash.se>
parents:
11100
diff
changeset
|
136 assert.same(normal.registry, compact1.registry); |
2288d206b14b
util.error: Expand compact registries into normal form internally
Kim Alvefur <zash@zash.se>
parents:
11100
diff
changeset
|
137 |
2288d206b14b
util.error: Expand compact registries into normal form internally
Kim Alvefur <zash@zash.se>
parents:
11100
diff
changeset
|
138 assert.same({ |
2288d206b14b
util.error: Expand compact registries into normal form internally
Kim Alvefur <zash@zash.se>
parents:
11100
diff
changeset
|
139 broke = {type = "cancel"; condition = "internal-server-error"; text = "It broke :("}; |
2288d206b14b
util.error: Expand compact registries into normal form internally
Kim Alvefur <zash@zash.se>
parents:
11100
diff
changeset
|
140 nope = {type = "auth"; condition = "not-authorized"; text = "Can't let you do that Dave"}; |
11102
5a0ff475ecfd
util.error: Drop registry initialization with namespace as key
Kim Alvefur <zash@zash.se>
parents:
11101
diff
changeset
|
141 }, compact2.registry); |
11101
2288d206b14b
util.error: Expand compact registries into normal form internally
Kim Alvefur <zash@zash.se>
parents:
11100
diff
changeset
|
142 end); |
11221
b0a563716334
util.error: Add coerce and wrap methods to registry(?) objects
Matthew Wild <mwild1@gmail.com>
parents:
11102
diff
changeset
|
143 |
b0a563716334
util.error: Add coerce and wrap methods to registry(?) objects
Matthew Wild <mwild1@gmail.com>
parents:
11102
diff
changeset
|
144 describe(".wrap", function () |
b0a563716334
util.error: Add coerce and wrap methods to registry(?) objects
Matthew Wild <mwild1@gmail.com>
parents:
11102
diff
changeset
|
145 local reg = errors.init("test", "spec", { |
b0a563716334
util.error: Add coerce and wrap methods to registry(?) objects
Matthew Wild <mwild1@gmail.com>
parents:
11102
diff
changeset
|
146 myerror = { "cancel", "internal-server-error", "Oh no" }; |
b0a563716334
util.error: Add coerce and wrap methods to registry(?) objects
Matthew Wild <mwild1@gmail.com>
parents:
11102
diff
changeset
|
147 }); |
b0a563716334
util.error: Add coerce and wrap methods to registry(?) objects
Matthew Wild <mwild1@gmail.com>
parents:
11102
diff
changeset
|
148 it("is exposed", function () |
b0a563716334
util.error: Add coerce and wrap methods to registry(?) objects
Matthew Wild <mwild1@gmail.com>
parents:
11102
diff
changeset
|
149 assert.is_function(reg.wrap); |
b0a563716334
util.error: Add coerce and wrap methods to registry(?) objects
Matthew Wild <mwild1@gmail.com>
parents:
11102
diff
changeset
|
150 end); |
b0a563716334
util.error: Add coerce and wrap methods to registry(?) objects
Matthew Wild <mwild1@gmail.com>
parents:
11102
diff
changeset
|
151 it("returns errors according to the registry", function () |
b0a563716334
util.error: Add coerce and wrap methods to registry(?) objects
Matthew Wild <mwild1@gmail.com>
parents:
11102
diff
changeset
|
152 local e = reg.wrap("myerror"); |
b0a563716334
util.error: Add coerce and wrap methods to registry(?) objects
Matthew Wild <mwild1@gmail.com>
parents:
11102
diff
changeset
|
153 assert.equal("cancel", e.type); |
b0a563716334
util.error: Add coerce and wrap methods to registry(?) objects
Matthew Wild <mwild1@gmail.com>
parents:
11102
diff
changeset
|
154 assert.equal("internal-server-error", e.condition); |
b0a563716334
util.error: Add coerce and wrap methods to registry(?) objects
Matthew Wild <mwild1@gmail.com>
parents:
11102
diff
changeset
|
155 assert.equal("Oh no", e.text); |
b0a563716334
util.error: Add coerce and wrap methods to registry(?) objects
Matthew Wild <mwild1@gmail.com>
parents:
11102
diff
changeset
|
156 end); |
b0a563716334
util.error: Add coerce and wrap methods to registry(?) objects
Matthew Wild <mwild1@gmail.com>
parents:
11102
diff
changeset
|
157 |
b0a563716334
util.error: Add coerce and wrap methods to registry(?) objects
Matthew Wild <mwild1@gmail.com>
parents:
11102
diff
changeset
|
158 it("passes through existing errors", function () |
b0a563716334
util.error: Add coerce and wrap methods to registry(?) objects
Matthew Wild <mwild1@gmail.com>
parents:
11102
diff
changeset
|
159 local e = reg.wrap(reg.new({ type = "auth", condition = "forbidden" })); |
b0a563716334
util.error: Add coerce and wrap methods to registry(?) objects
Matthew Wild <mwild1@gmail.com>
parents:
11102
diff
changeset
|
160 assert.equal("auth", e.type); |
b0a563716334
util.error: Add coerce and wrap methods to registry(?) objects
Matthew Wild <mwild1@gmail.com>
parents:
11102
diff
changeset
|
161 assert.equal("forbidden", e.condition); |
b0a563716334
util.error: Add coerce and wrap methods to registry(?) objects
Matthew Wild <mwild1@gmail.com>
parents:
11102
diff
changeset
|
162 end); |
b0a563716334
util.error: Add coerce and wrap methods to registry(?) objects
Matthew Wild <mwild1@gmail.com>
parents:
11102
diff
changeset
|
163 |
b0a563716334
util.error: Add coerce and wrap methods to registry(?) objects
Matthew Wild <mwild1@gmail.com>
parents:
11102
diff
changeset
|
164 it("wraps arbitrary values", function () |
b0a563716334
util.error: Add coerce and wrap methods to registry(?) objects
Matthew Wild <mwild1@gmail.com>
parents:
11102
diff
changeset
|
165 local e = reg.wrap(123); |
b0a563716334
util.error: Add coerce and wrap methods to registry(?) objects
Matthew Wild <mwild1@gmail.com>
parents:
11102
diff
changeset
|
166 assert.equal("cancel", e.type); |
b0a563716334
util.error: Add coerce and wrap methods to registry(?) objects
Matthew Wild <mwild1@gmail.com>
parents:
11102
diff
changeset
|
167 assert.equal("undefined-condition", e.condition); |
b0a563716334
util.error: Add coerce and wrap methods to registry(?) objects
Matthew Wild <mwild1@gmail.com>
parents:
11102
diff
changeset
|
168 assert.equal(123, e.context.wrapped_error); |
b0a563716334
util.error: Add coerce and wrap methods to registry(?) objects
Matthew Wild <mwild1@gmail.com>
parents:
11102
diff
changeset
|
169 end); |
b0a563716334
util.error: Add coerce and wrap methods to registry(?) objects
Matthew Wild <mwild1@gmail.com>
parents:
11102
diff
changeset
|
170 end); |
b0a563716334
util.error: Add coerce and wrap methods to registry(?) objects
Matthew Wild <mwild1@gmail.com>
parents:
11102
diff
changeset
|
171 |
b0a563716334
util.error: Add coerce and wrap methods to registry(?) objects
Matthew Wild <mwild1@gmail.com>
parents:
11102
diff
changeset
|
172 describe(".coerce", function () |
b0a563716334
util.error: Add coerce and wrap methods to registry(?) objects
Matthew Wild <mwild1@gmail.com>
parents:
11102
diff
changeset
|
173 local reg = errors.init("test", "spec", { |
b0a563716334
util.error: Add coerce and wrap methods to registry(?) objects
Matthew Wild <mwild1@gmail.com>
parents:
11102
diff
changeset
|
174 myerror = { "cancel", "internal-server-error", "Oh no" }; |
b0a563716334
util.error: Add coerce and wrap methods to registry(?) objects
Matthew Wild <mwild1@gmail.com>
parents:
11102
diff
changeset
|
175 }); |
b0a563716334
util.error: Add coerce and wrap methods to registry(?) objects
Matthew Wild <mwild1@gmail.com>
parents:
11102
diff
changeset
|
176 |
b0a563716334
util.error: Add coerce and wrap methods to registry(?) objects
Matthew Wild <mwild1@gmail.com>
parents:
11102
diff
changeset
|
177 it("is exposed", function () |
b0a563716334
util.error: Add coerce and wrap methods to registry(?) objects
Matthew Wild <mwild1@gmail.com>
parents:
11102
diff
changeset
|
178 assert.is_function(reg.coerce); |
b0a563716334
util.error: Add coerce and wrap methods to registry(?) objects
Matthew Wild <mwild1@gmail.com>
parents:
11102
diff
changeset
|
179 end); |
b0a563716334
util.error: Add coerce and wrap methods to registry(?) objects
Matthew Wild <mwild1@gmail.com>
parents:
11102
diff
changeset
|
180 |
b0a563716334
util.error: Add coerce and wrap methods to registry(?) objects
Matthew Wild <mwild1@gmail.com>
parents:
11102
diff
changeset
|
181 it("passes through existing errors", function () |
b0a563716334
util.error: Add coerce and wrap methods to registry(?) objects
Matthew Wild <mwild1@gmail.com>
parents:
11102
diff
changeset
|
182 local function test() |
b0a563716334
util.error: Add coerce and wrap methods to registry(?) objects
Matthew Wild <mwild1@gmail.com>
parents:
11102
diff
changeset
|
183 return nil, errors.new({ type = "auth", condition = "forbidden" }); |
b0a563716334
util.error: Add coerce and wrap methods to registry(?) objects
Matthew Wild <mwild1@gmail.com>
parents:
11102
diff
changeset
|
184 end |
b0a563716334
util.error: Add coerce and wrap methods to registry(?) objects
Matthew Wild <mwild1@gmail.com>
parents:
11102
diff
changeset
|
185 local ok, err = reg.coerce(test()); |
b0a563716334
util.error: Add coerce and wrap methods to registry(?) objects
Matthew Wild <mwild1@gmail.com>
parents:
11102
diff
changeset
|
186 assert.is_nil(ok); |
b0a563716334
util.error: Add coerce and wrap methods to registry(?) objects
Matthew Wild <mwild1@gmail.com>
parents:
11102
diff
changeset
|
187 assert.is_truthy(errors.is_err(err)); |
b0a563716334
util.error: Add coerce and wrap methods to registry(?) objects
Matthew Wild <mwild1@gmail.com>
parents:
11102
diff
changeset
|
188 assert.equal("forbidden", err.condition); |
b0a563716334
util.error: Add coerce and wrap methods to registry(?) objects
Matthew Wild <mwild1@gmail.com>
parents:
11102
diff
changeset
|
189 end); |
b0a563716334
util.error: Add coerce and wrap methods to registry(?) objects
Matthew Wild <mwild1@gmail.com>
parents:
11102
diff
changeset
|
190 |
b0a563716334
util.error: Add coerce and wrap methods to registry(?) objects
Matthew Wild <mwild1@gmail.com>
parents:
11102
diff
changeset
|
191 it("passes through successful return values", function () |
b0a563716334
util.error: Add coerce and wrap methods to registry(?) objects
Matthew Wild <mwild1@gmail.com>
parents:
11102
diff
changeset
|
192 local function test() |
b0a563716334
util.error: Add coerce and wrap methods to registry(?) objects
Matthew Wild <mwild1@gmail.com>
parents:
11102
diff
changeset
|
193 return 1, 2, 3, 4; |
b0a563716334
util.error: Add coerce and wrap methods to registry(?) objects
Matthew Wild <mwild1@gmail.com>
parents:
11102
diff
changeset
|
194 end |
b0a563716334
util.error: Add coerce and wrap methods to registry(?) objects
Matthew Wild <mwild1@gmail.com>
parents:
11102
diff
changeset
|
195 local one, two, three, four = reg.coerce(test()); |
b0a563716334
util.error: Add coerce and wrap methods to registry(?) objects
Matthew Wild <mwild1@gmail.com>
parents:
11102
diff
changeset
|
196 assert.equal(1, one); |
b0a563716334
util.error: Add coerce and wrap methods to registry(?) objects
Matthew Wild <mwild1@gmail.com>
parents:
11102
diff
changeset
|
197 assert.equal(2, two); |
b0a563716334
util.error: Add coerce and wrap methods to registry(?) objects
Matthew Wild <mwild1@gmail.com>
parents:
11102
diff
changeset
|
198 assert.equal(3, three); |
b0a563716334
util.error: Add coerce and wrap methods to registry(?) objects
Matthew Wild <mwild1@gmail.com>
parents:
11102
diff
changeset
|
199 assert.equal(4, four); |
b0a563716334
util.error: Add coerce and wrap methods to registry(?) objects
Matthew Wild <mwild1@gmail.com>
parents:
11102
diff
changeset
|
200 end); |
b0a563716334
util.error: Add coerce and wrap methods to registry(?) objects
Matthew Wild <mwild1@gmail.com>
parents:
11102
diff
changeset
|
201 |
b0a563716334
util.error: Add coerce and wrap methods to registry(?) objects
Matthew Wild <mwild1@gmail.com>
parents:
11102
diff
changeset
|
202 it("wraps non-error objects", function () |
b0a563716334
util.error: Add coerce and wrap methods to registry(?) objects
Matthew Wild <mwild1@gmail.com>
parents:
11102
diff
changeset
|
203 local function test() |
b0a563716334
util.error: Add coerce and wrap methods to registry(?) objects
Matthew Wild <mwild1@gmail.com>
parents:
11102
diff
changeset
|
204 return nil, "myerror"; |
b0a563716334
util.error: Add coerce and wrap methods to registry(?) objects
Matthew Wild <mwild1@gmail.com>
parents:
11102
diff
changeset
|
205 end |
b0a563716334
util.error: Add coerce and wrap methods to registry(?) objects
Matthew Wild <mwild1@gmail.com>
parents:
11102
diff
changeset
|
206 local ok, err = reg.coerce(test()); |
b0a563716334
util.error: Add coerce and wrap methods to registry(?) objects
Matthew Wild <mwild1@gmail.com>
parents:
11102
diff
changeset
|
207 assert.is_nil(ok); |
b0a563716334
util.error: Add coerce and wrap methods to registry(?) objects
Matthew Wild <mwild1@gmail.com>
parents:
11102
diff
changeset
|
208 assert.is_truthy(errors.is_err(err)); |
b0a563716334
util.error: Add coerce and wrap methods to registry(?) objects
Matthew Wild <mwild1@gmail.com>
parents:
11102
diff
changeset
|
209 assert.equal("internal-server-error", err.condition); |
b0a563716334
util.error: Add coerce and wrap methods to registry(?) objects
Matthew Wild <mwild1@gmail.com>
parents:
11102
diff
changeset
|
210 assert.equal("Oh no", err.text); |
b0a563716334
util.error: Add coerce and wrap methods to registry(?) objects
Matthew Wild <mwild1@gmail.com>
parents:
11102
diff
changeset
|
211 end); |
b0a563716334
util.error: Add coerce and wrap methods to registry(?) objects
Matthew Wild <mwild1@gmail.com>
parents:
11102
diff
changeset
|
212 end); |
11097
f23cf8e2e2ff
util.error: Cover registry initialization in test
Kim Alvefur <zash@zash.se>
parents:
11092
diff
changeset
|
213 end); |
f23cf8e2e2ff
util.error: Cover registry initialization in test
Kim Alvefur <zash@zash.se>
parents:
11092
diff
changeset
|
214 |
10101 | 215 end); |
216 |