View | Details | Raw Unified | Return to bug 22343
Collapse All | Expand All

(-)a/Koha/REST/V1/Config/SMTP/Servers.pm (+194 lines)
Line 0 Link Here
1
package Koha::REST::V1::Config::SMTP::Servers;
2
3
# This file is part of Koha.
4
#
5
# Koha is free software; you can redistribute it and/or modify it
6
# under the terms of the GNU General Public License as published by
7
# the Free Software Foundation; either version 3 of the License, or
8
# (at your option) any later version.
9
#
10
# Koha is distributed in the hope that it will be useful, but
11
# WITHOUT ANY WARRANTY; without even the implied warranty of
12
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
# GNU General Public License for more details.
14
#
15
# You should have received a copy of the GNU General Public License
16
# along with Koha; if not, see <http://www.gnu.org/licenses>.
17
18
use Modern::Perl;
19
20
use Mojo::Base 'Mojolicious::Controller';
21
22
use Koha::SMTP::Servers;
23
24
use Try::Tiny;
25
26
=head1 API
27
28
=head2 Methods
29
30
=head3 list
31
32
Controller method that handles listing Koha::SMTP::Server objects
33
34
=cut
35
36
sub list {
37
    my $c = shift->openapi->valid_input or return;
38
39
    return try {
40
        my $smtp_servers_set = Koha::SMTP::Servers->new;
41
        my $smtp_servers = $c->objects->search( $smtp_servers_set );
42
        return $c->render(
43
            status  => 200,
44
            openapi => $smtp_servers
45
        );
46
    }
47
    catch {
48
        $c->unhandled_exception($_);
49
    };
50
}
51
52
=head3 get
53
54
Controller method that handles retrieving a single Koha::SMTP::Server object
55
56
=cut
57
58
sub get {
59
    my $c = shift->openapi->valid_input or return;
60
61
    return try {
62
        my $smtp_server = Koha::SMTP::Servers->find( $c->validation->param('smtp_server_id') );
63
64
        unless ($smtp_server) {
65
            return $c->render(
66
                status  => 404,
67
                openapi => {
68
                    error => "SMTP server not found"
69
                }
70
            );
71
        }
72
73
        my $embed = $c->stash('koha.embed');
74
75
        return $c->render(
76
            status  => 200,
77
            openapi => $smtp_server->to_api({ embed => $embed })
78
        );
79
    }
80
    catch {
81
        $c->unhandled_exception($_);
82
    }
83
}
84
85
=head3 add
86
87
Controller method that handles adding a new Koha::SMTP::Server object
88
89
=cut
90
91
sub add {
92
    my $c = shift->openapi->valid_input or return;
93
94
    return try {
95
96
        my $smtp_server = Koha::SMTP::Server->new_from_api( $c->validation->param('body') );
97
        $smtp_server->store->discard_changes;
98
99
        $c->res->headers->location( $c->req->url->to_string . '/' . $smtp_server->id );
100
101
        return $c->render(
102
            status  => 201,
103
            openapi => $smtp_server->to_api
104
        );
105
    }
106
    catch {
107
        if ( blessed $_ and $_->isa('Koha::Exceptions::Object::DuplicateID') ) {
108
            return $c->render(
109
                status  => 409,
110
                openapi => {
111
                    error    => $_->error,
112
                    conflict => $_->duplicate_id
113
                }
114
            );
115
        }
116
117
        $c->unhandled_exception($_);
118
    };
119
}
120
121
=head3 update
122
123
Controller method that handles updating a Koha::SMTP::Server object
124
125
=cut
126
127
sub update {
128
    my $c = shift->openapi->valid_input or return;
129
130
    my $smtp_server = Koha::SMTP::Servers->find( $c->validation->param('smtp_server_id') );
131
132
    if ( not defined $smtp_server ) {
133
        return $c->render(
134
            status  => 404,
135
            openapi => {
136
                error => "Object not found"
137
            }
138
        );
139
    }
140
141
    return try {
142
        $smtp_server->set_from_api( $c->validation->param('body') );
143
        $smtp_server->store->discard_changes;
144
145
        return $c->render(
146
            status  => 200,
147
            openapi => $smtp_server->to_api
148
        );
149
    }
150
    catch {
151
        if ( blessed $_ and $_->isa('Koha::Exceptions::Object::DuplicateID') ) {
152
            return $c->render(
153
                status  => 409,
154
                openapi => {
155
                    error    => $_->error,
156
                    conflict => $_->duplicate_id
157
                }
158
            );
159
        }
160
161
        $c->unhandled_exception($_);
162
    };
163
}
164
165
=head3 delete
166
167
Controller method that handles deleting a Koha::SMTP::Server object
168
169
=cut
170
171
sub delete {
172
    my $c = shift->openapi->valid_input or return;
173
174
    my $smtp_server = Koha::SMTP::Servers->find( $c->validation->param('smtp_server_id') );
175
176
    if ( not defined $smtp_server ) {
177
        return $c->render( status  => 404,
178
                           openapi => { error => "Object not found" } );
179
    }
180
181
    return try {
182
        $smtp_server->delete;
183
184
        return $c->render(
185
            status  => 204,
186
            openapi => q{}
187
        );
188
    }
189
    catch {
190
        $c->unhandled_exception($_);
191
    };
192
}
193
194
1;
(-)a/api/v1/swagger/definitions.json (+3 lines)
Lines 67-71 Link Here
67
  },
67
  },
68
  "return_claim": {
68
  "return_claim": {
69
    "$ref": "definitions/return_claim.json"
69
    "$ref": "definitions/return_claim.json"
70
  },
71
  "smtp_server": {
72
    "$ref": "definitions/smtp_server.json"
70
  }
73
  }
71
}
74
}
(-)a/api/v1/swagger/definitions/smtp_server.json (+71 lines)
Line 0 Link Here
1
{
2
  "type": "object",
3
  "properties": {
4
    "smtp_server_id": {
5
      "type": "integer",
6
      "description": "Internal SMTP server identifier",
7
      "readOnly": true
8
    },
9
    "name": {
10
      "type": "string",
11
      "description": "Name of the SMTP server"
12
    },
13
    "library_id": {
14
      "type": [
15
        "string",
16
        "null"
17
      ],
18
      "description": "Internal identifier for the library using it. null means global"
19
    },
20
    "host": {
21
      "type": "string",
22
      "description": "SMTP host name"
23
    },
24
    "port": {
25
      "type": "integer",
26
      "description": "TCP port number"
27
    },
28
    "timeout": {
29
      "type": "integer",
30
      "description": "Maximum time in seconds to wait for server"
31
    },
32
    "ssl_mode": {
33
      "type": "string",
34
      "enum": [
35
        "disabled",
36
        "ssl",
37
        "starttls"
38
      ],
39
      "description": "If SSL/TLS will be used"
40
    },
41
    "user_name": {
42
      "type": [
43
        "string",
44
        "null"
45
      ],
46
      "description": "The user name to use for authentication (optional)"
47
    },
48
    "password": {
49
      "type": [
50
        "string",
51
        "null"
52
      ],
53
      "description": "The password to use for authentication (optional)"
54
    },
55
    "debug": {
56
      "type": "boolean",
57
      "description": "If the SMTP connection is set to debug mode"
58
    },
59
    "library": {
60
      "type": [
61
        "object",
62
        "null"
63
      ],
64
      "description": "Embedded library object (x-koha-embed)"
65
    }
66
  },
67
  "additionalProperties": false,
68
  "required": [
69
    "name"
70
  ]
71
}
(-)a/api/v1/swagger/parameters.json (+3 lines)
Lines 29-34 Link Here
29
  "order_id_pp": {
29
  "order_id_pp": {
30
    "$ref": "parameters/order.json#/order_id_pp"
30
    "$ref": "parameters/order.json#/order_id_pp"
31
  },
31
  },
32
  "smtp_server_id_pp": {
33
    "$ref": "parameters/smtp_server.json#/smtp_server_id_pp"
34
  },
32
  "vendoridPathParam": {
35
  "vendoridPathParam": {
33
    "$ref": "parameters/vendor.json#/vendoridPathParam"
36
    "$ref": "parameters/vendor.json#/vendoridPathParam"
34
  },
37
  },
(-)a/api/v1/swagger/parameters/smtp_server.json (+9 lines)
Line 0 Link Here
1
{
2
    "smtp_server_id_pp": {
3
      "name": "smtp_server_id",
4
      "in": "path",
5
      "description": "SMTP server internal identifier",
6
      "required": true,
7
      "type": "integer"
8
    }
9
}
(-)a/api/v1/swagger/paths.json (+6 lines)
Lines 41-46 Link Here
41
  "/clubs/{club_id}/holds": {
41
  "/clubs/{club_id}/holds": {
42
    "$ref": "paths/clubs.json#/~1clubs~1{club_id}~1holds"
42
    "$ref": "paths/clubs.json#/~1clubs~1{club_id}~1holds"
43
  },
43
  },
44
  "/config/smtp_servers": {
45
    "$ref": "paths/config_smtp_servers.json#/~1config~1smtp_servers"
46
  },
47
  "/config/smtp_servers/{smtp_server_id}": {
48
    "$ref": "paths/config_smtp_servers.json#/~1config~1smtp_servers~1{smtp_server_id}"
49
  },
44
  "/holds": {
50
  "/holds": {
45
    "$ref": "paths/holds.json#/~1holds"
51
    "$ref": "paths/holds.json#/~1holds"
46
  },
52
  },
(-)a/api/v1/swagger/paths/config_smtp_servers.json (+319 lines)
Line 0 Link Here
1
{
2
  "/config/smtp_servers": {
3
    "get": {
4
      "x-mojo-to": "Config::SMTP::Servers#list",
5
      "operationId": "listSMTPServers",
6
      "tags": [
7
        "config",
8
        "smtp"
9
      ],
10
      "produces": [
11
        "application/json"
12
      ],
13
      "parameters": [
14
        {
15
          "$ref": "../parameters.json#/match"
16
        },
17
        {
18
          "$ref": "../parameters.json#/order_by"
19
        },
20
        {
21
          "$ref": "../parameters.json#/page"
22
        },
23
        {
24
          "$ref": "../parameters.json#/per_page"
25
        },
26
        {
27
          "$ref": "../parameters.json#/q_param"
28
        },
29
        {
30
          "$ref": "../parameters.json#/q_body"
31
        },
32
        {
33
          "$ref": "../parameters.json#/q_header"
34
        }
35
      ],
36
      "responses": {
37
        "200": {
38
          "description": "A list of SMTP servers",
39
          "schema": {
40
            "type": "array",
41
            "items": {
42
              "$ref": "../definitions.json#/smtp_server"
43
            }
44
          }
45
        },
46
        "403": {
47
          "description": "Access forbidden",
48
          "schema": {
49
            "$ref": "../definitions.json#/error"
50
          }
51
        },
52
        "500": {
53
          "description": "Internal error",
54
          "schema": {
55
            "$ref": "../definitions.json#/error"
56
          }
57
        },
58
        "503": {
59
          "description": "Under maintenance",
60
          "schema": {
61
            "$ref": "../definitions.json#/error"
62
          }
63
        }
64
      },
65
      "x-koha-authorization": {
66
        "permissions": {
67
          "parameters": "1"
68
        }
69
      },
70
      "x-koha-embed": [
71
        "library"
72
      ]
73
    },
74
    "post": {
75
      "x-mojo-to": "Config::SMTP::Servers#add",
76
      "operationId": "addSMTPServer",
77
      "tags": [
78
        "config",
79
        "smtp"
80
      ],
81
      "parameters": [
82
        {
83
          "name": "body",
84
          "in": "body",
85
          "description": "A JSON object representing a new SMTP server configuration",
86
          "required": true,
87
          "schema": {
88
            "$ref": "../definitions.json#/smtp_server"
89
          }
90
        }
91
      ],
92
      "produces": [
93
        "application/json"
94
      ],
95
      "responses": {
96
        "201": {
97
          "description": "An SMTP server object",
98
          "schema": {
99
            "$ref": "../definitions.json#/smtp_server"
100
          }
101
        },
102
        "401": {
103
          "description": "Authentication required",
104
          "schema": {
105
            "$ref": "../definitions.json#/error"
106
          }
107
        },
108
        "403": {
109
          "description": "Access forbidden",
110
          "schema": {
111
            "$ref": "../definitions.json#/error"
112
          }
113
        },
114
        "409": {
115
          "description": "Conflict in creating resource",
116
          "schema": {
117
            "$ref": "../definitions.json#/error"
118
          }
119
        },
120
        "500": {
121
          "description": "Internal error",
122
          "schema": {
123
            "$ref": "../definitions.json#/error"
124
          }
125
        },
126
        "503": {
127
          "description": "Under maintenance",
128
          "schema": {
129
            "$ref": "../definitions.json#/error"
130
          }
131
        }
132
      },
133
      "x-koha-authorization": {
134
        "permissions": {
135
          "parameters": "1"
136
        }
137
      }
138
    }
139
  },
140
  "/config/smtp_servers/{smtp_server_id}": {
141
    "get": {
142
      "x-mojo-to": "Config::SMTP::Servers#get",
143
      "operationId": "getSMTPServer",
144
      "tags": [
145
        "config",
146
        "smtp"
147
      ],
148
      "parameters": [
149
        {
150
          "$ref": "../parameters.json#/smtp_server_id_pp"
151
        }
152
      ],
153
      "produces": [
154
        "application/json"
155
      ],
156
      "responses": {
157
        "200": {
158
          "description": "An SMTP server object",
159
          "schema": {
160
            "$ref": "../definitions.json#/smtp_server"
161
          }
162
        },
163
        "404": {
164
          "description": "Object not found",
165
          "schema": {
166
            "$ref": "../definitions.json#/error"
167
          }
168
        },
169
        "409": {
170
          "description": "Conflict updating resource",
171
          "schema": {
172
            "$ref": "../definitions.json#/error"
173
          }
174
        },
175
        "500": {
176
          "description": "Internal error",
177
          "schema": {
178
            "$ref": "../definitions.json#/error"
179
          }
180
        },
181
        "503": {
182
          "description": "Under maintenance",
183
          "schema": {
184
            "$ref": "../definitions.json#/error"
185
          }
186
        }
187
      },
188
      "x-koha-authorization": {
189
        "permissions": {
190
          "parameters": "1"
191
        }
192
      }
193
    },
194
    "put": {
195
      "x-mojo-to": "Config::SMTP::Servers#update",
196
      "operationId": "updateSMTPServer",
197
      "tags": [
198
        "config",
199
        "smtp"
200
      ],
201
      "parameters": [
202
        {
203
          "$ref": "../parameters.json#/smtp_server_id_pp"
204
        },
205
        {
206
          "name": "body",
207
          "in": "body",
208
          "description": "An SMTP server object",
209
          "required": true,
210
          "schema": {
211
            "$ref": "../definitions.json#/smtp_server"
212
          }
213
        }
214
      ],
215
      "produces": [
216
        "application/json"
217
      ],
218
      "responses": {
219
        "200": {
220
          "description": "An SMTP server object",
221
          "schema": {
222
            "$ref": "../definitions.json#/smtp_server"
223
          }
224
        },
225
        "401": {
226
          "description": "Authentication required",
227
          "schema": {
228
            "$ref": "../definitions.json#/error"
229
          }
230
        },
231
        "403": {
232
          "description": "Access forbidden",
233
          "schema": {
234
            "$ref": "../definitions.json#/error"
235
          }
236
        },
237
        "404": {
238
          "description": "Object not found",
239
          "schema": {
240
            "$ref": "../definitions.json#/error"
241
          }
242
        },
243
        "500": {
244
          "description": "Internal error",
245
          "schema": {
246
            "$ref": "../definitions.json#/error"
247
          }
248
        },
249
        "503": {
250
          "description": "Under maintenance",
251
          "schema": {
252
            "$ref": "../definitions.json#/error"
253
          }
254
        }
255
      },
256
      "x-koha-authorization": {
257
        "permissions": {
258
          "parameters": "1"
259
        }
260
      }
261
    },
262
    "delete": {
263
      "x-mojo-to": "Config::SMTP::Servers#delete",
264
      "operationId": "deleteSMTPServer",
265
      "tags": [
266
        "config",
267
        "smtp"
268
      ],
269
      "parameters": [
270
        {
271
          "$ref": "../parameters.json#/smtp_server_id_pp"
272
        }
273
      ],
274
      "produces": [
275
        "application/json"
276
      ],
277
      "responses": {
278
        "204": {
279
          "description": "SMTP server deleted"
280
        },
281
        "401": {
282
          "description": "Authentication required",
283
          "schema": {
284
            "$ref": "../definitions.json#/error"
285
          }
286
        },
287
        "403": {
288
          "description": "Access forbidden",
289
          "schema": {
290
            "$ref": "../definitions.json#/error"
291
          }
292
        },
293
        "404": {
294
          "description": "Object not found",
295
          "schema": {
296
            "$ref": "../definitions.json#/error"
297
          }
298
        },
299
        "500": {
300
          "description": "Internal error",
301
          "schema": {
302
            "$ref": "../definitions.json#/error"
303
          }
304
        },
305
        "503": {
306
          "description": "Under maintenance",
307
          "schema": {
308
            "$ref": "../definitions.json#/error"
309
          }
310
        }
311
      },
312
      "x-koha-authorization": {
313
        "permissions": {
314
          "parameters": "1"
315
        }
316
      }
317
    }
318
  }
319
}
(-)a/t/db_dependent/api/v1/smtp_servers.t (-1 / +358 lines)
Line 0 Link Here
0
- 
1
#!/usr/bin/env perl
2
3
# This file is part of Koha.
4
#
5
# Koha is free software; you can redistribute it and/or modify it
6
# under the terms of the GNU General Public License as published by
7
# the Free Software Foundation; either version 3 of the License, or
8
# (at your option) any later version.
9
#
10
# Koha is distributed in the hope that it will be useful, but
11
# WITHOUT ANY WARRANTY; without even the implied warranty of
12
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
# GNU General Public License for more details.
14
#
15
# You should have received a copy of the GNU General Public License
16
# along with Koha; if not, see <http://www.gnu.org/licenses>.
17
18
use Modern::Perl;
19
20
use Test::More tests => 5;
21
use Test::Mojo;
22
23
use t::lib::TestBuilder;
24
use t::lib::Mocks;
25
26
use Koha::SMTP::Servers;
27
use Koha::Database;
28
29
my $schema  = Koha::Database->new->schema;
30
my $builder = t::lib::TestBuilder->new;
31
32
my $t = Test::Mojo->new('Koha::REST::V1');
33
t::lib::Mocks::mock_preference( 'RESTBasicAuth', 1 );
34
35
subtest 'list() tests' => sub {
36
37
    plan tests => 11;
38
39
    $schema->storage->txn_begin;
40
41
    Koha::SMTP::Servers->search->delete;
42
43
    my $librarian = $builder->build_object(
44
        {
45
            class => 'Koha::Patrons',
46
            value => { flags => 3**2 }    # parameters flag = 3
47
        }
48
    );
49
    my $password = 'thePassword123';
50
    $librarian->set_password( { password => $password, skip_validation => 1 } );
51
    my $userid = $librarian->userid;
52
53
    my $patron = $builder->build_object(
54
        {
55
            class => 'Koha::Patrons',
56
            value => { flags => 0 }
57
        }
58
    );
59
60
    $patron->set_password( { password => $password, skip_validation => 1 } );
61
    my $unauth_userid = $patron->userid;
62
63
    ## Authorized user tests
64
    # No SMTP servers, so empty array should be returned
65
    $t->get_ok("//$userid:$password@/api/v1/config/smtp_servers")
66
      ->status_is(200)->json_is( [] );
67
68
    my $smtp_server =
69
      $builder->build_object( { class => 'Koha::SMTP::Servers' } );
70
71
    # One city created, should get returned
72
    $t->get_ok("//$userid:$password@/api/v1/config/smtp_servers")
73
      ->status_is(200)->json_is( [ $smtp_server->to_api ] );
74
75
    my $another_smtp_server =
76
      $builder->build_object( { class => 'Koha::SMTP::Servers' } );
77
78
    # Two SMTP servers created, they should both be returned
79
    $t->get_ok("//$userid:$password@/api/v1/config/smtp_servers")
80
      ->status_is(200)
81
      ->json_is( [ $smtp_server->to_api, $another_smtp_server->to_api, ] );
82
83
    # Unauthorized access
84
    $t->get_ok("//$unauth_userid:$password@/api/v1/config/smtp_servers")
85
      ->status_is(403);
86
87
    $schema->storage->txn_rollback;
88
};
89
90
subtest 'get() tests' => sub {
91
92
    plan tests => 8;
93
94
    $schema->storage->txn_begin;
95
96
    my $smtp_server =
97
      $builder->build_object( { class => 'Koha::SMTP::Servers' } );
98
    my $librarian = $builder->build_object(
99
        {
100
            class => 'Koha::Patrons',
101
            value => { flags => 3**2 }    # parameters flag = 3
102
        }
103
    );
104
    my $password = 'thePassword123';
105
    $librarian->set_password( { password => $password, skip_validation => 1 } );
106
    my $userid = $librarian->userid;
107
108
    my $patron = $builder->build_object(
109
        {
110
            class => 'Koha::Patrons',
111
            value => { flags => 0 }
112
        }
113
    );
114
115
    $patron->set_password( { password => $password, skip_validation => 1 } );
116
    my $unauth_userid = $patron->userid;
117
118
    $t->get_ok(
119
        "//$userid:$password@/api/v1/config/smtp_servers/" . $smtp_server->id )
120
      ->status_is(200)->json_is( $smtp_server->to_api );
121
122
    $t->get_ok( "//$unauth_userid:$password@/api/v1/config/smtp_servers/"
123
          . $smtp_server->id )->status_is(403);
124
125
    my $smtp_server_to_delete =
126
      $builder->build_object( { class => 'Koha::SMTP::Servers' } );
127
    my $non_existent_id = $smtp_server_to_delete->id;
128
    $smtp_server_to_delete->delete;
129
130
    $t->get_ok(
131
        "//$userid:$password@/api/v1/config/smtp_servers/$non_existent_id")
132
      ->status_is(404)->json_is( '/error' => 'SMTP server not found' );
133
134
    $schema->storage->txn_rollback;
135
};
136
137
subtest 'add() tests' => sub {
138
139
    plan tests => 18;
140
141
    $schema->storage->txn_begin;
142
143
    Koha::SMTP::Servers->search->delete;
144
145
    my $librarian = $builder->build_object(
146
        {
147
            class => 'Koha::Patrons',
148
            value => { flags => 3**2 }    # parameters flag = 3
149
        }
150
    );
151
    my $password = 'thePassword123';
152
    $librarian->set_password( { password => $password, skip_validation => 1 } );
153
    my $userid = $librarian->userid;
154
155
    my $patron = $builder->build_object(
156
        {
157
            class => 'Koha::Patrons',
158
            value => { flags => 0 }
159
        }
160
    );
161
162
    $patron->set_password( { password => $password, skip_validation => 1 } );
163
    my $unauth_userid = $patron->userid;
164
165
    my $smtp_server =
166
      $builder->build_object( { class => 'Koha::SMTP::Servers' } );
167
    my $smtp_server_data = $smtp_server->to_api;
168
    delete $smtp_server_data->{smtp_server_id};
169
    $smtp_server->delete;
170
171
    # Unauthorized attempt to write
172
    $t->post_ok(
173
        "//$unauth_userid:$password@/api/v1/config/smtp_servers" => json =>
174
          $smtp_server_data )->status_is(403);
175
176
    # Authorized attempt to write invalid data
177
    my $smtp_server_with_invalid_field = {
178
        name => 'Some other server',
179
        blah => 'blah'
180
    };
181
182
    $t->post_ok( "//$userid:$password@/api/v1/config/smtp_servers" => json =>
183
          $smtp_server_with_invalid_field )->status_is(400)->json_is(
184
        "/errors" => [
185
            {
186
                message => "Properties not allowed: blah.",
187
                path    => "/body"
188
            }
189
        ]
190
          );
191
192
    # Authorized attempt to write
193
    my $smtp_server_id =
194
      $t->post_ok( "//$userid:$password@/api/v1/config/smtp_servers" => json =>
195
          $smtp_server_data )->status_is( 201, 'SWAGGER3.2.1' )->header_like(
196
        Location => qr|^\/api\/v1\/config\/smtp_servers\/\d*|,
197
        'SWAGGER3.4.1'
198
    )->json_is( '/name' => $smtp_server_data->{name} )
199
      ->json_is( '/state'       => $smtp_server_data->{state} )
200
      ->json_is( '/postal_code' => $smtp_server_data->{postal_code} )
201
      ->json_is( '/country'     => $smtp_server_data->{country} )
202
      ->tx->res->json->{smtp_server_id};
203
204
    # Authorized attempt to create with null id
205
    $smtp_server_data->{smtp_server_id} = undef;
206
    $t->post_ok( "//$userid:$password@/api/v1/config/smtp_servers" => json =>
207
          $smtp_server_data )->status_is(400)->json_has('/errors');
208
209
    # Authorized attempt to create with existing id
210
    $smtp_server_data->{smtp_server_id} = $smtp_server_id;
211
    $t->post_ok( "//$userid:$password@/api/v1/config/smtp_servers" => json =>
212
          $smtp_server_data )->status_is(400)->json_is(
213
        "/errors" => [
214
            {
215
                message => "Read-only.",
216
                path    => "/body/smtp_server_id"
217
            }
218
        ]
219
          );
220
221
    $schema->storage->txn_rollback;
222
};
223
224
subtest 'update() tests' => sub {
225
226
    plan tests => 15;
227
228
    $schema->storage->txn_begin;
229
230
    my $librarian = $builder->build_object(
231
        {
232
            class => 'Koha::Patrons',
233
            value => { flags => 3**2 }    # parameters flag = 3
234
        }
235
    );
236
    my $password = 'thePassword123';
237
    $librarian->set_password( { password => $password, skip_validation => 1 } );
238
    my $userid = $librarian->userid;
239
240
    my $patron = $builder->build_object(
241
        {
242
            class => 'Koha::Patrons',
243
            value => { flags => 0 }
244
        }
245
    );
246
247
    $patron->set_password( { password => $password, skip_validation => 1 } );
248
    my $unauth_userid = $patron->userid;
249
250
    my $smtp_server_id =
251
      $builder->build_object( { class => 'Koha::SMTP::Servers' } )->id;
252
253
    # Unauthorized attempt to update
254
    $t->put_ok(
255
        "//$unauth_userid:$password@/api/v1/config/smtp_servers/$smtp_server_id"
256
          => json => { name => 'New unauthorized name change' } )
257
      ->status_is(403);
258
259
    # Attempt partial update on a PUT
260
    my $smtp_server_with_missing_field = {
261
        host     => 'localhost',
262
        ssl_mode => 'disabled'
263
    };
264
265
    $t->put_ok(
266
        "//$userid:$password@/api/v1/config/smtp_servers/$smtp_server_id" =>
267
          json => $smtp_server_with_missing_field )->status_is(400)
268
      ->json_is( "/errors" =>
269
          [ { message => "Missing property.", path => "/body/name" } ] );
270
271
    # Full object update on PUT
272
    my $smtp_server_with_updated_field = { name => "Some name", };
273
274
    $t->put_ok(
275
        "//$userid:$password@/api/v1/config/smtp_servers/$smtp_server_id" =>
276
          json => $smtp_server_with_updated_field )->status_is(200)
277
      ->json_is( '/name' => 'Some name' );
278
279
    # Authorized attempt to write invalid data
280
    my $smtp_server_with_invalid_field = {
281
        blah => "Blah",
282
        name => 'Some name'
283
    };
284
285
    $t->put_ok(
286
        "//$userid:$password@/api/v1/config/smtp_servers/$smtp_server_id" =>
287
          json => $smtp_server_with_invalid_field )->status_is(400)->json_is(
288
        "/errors" => [
289
            {
290
                message => "Properties not allowed: blah.",
291
                path    => "/body"
292
            }
293
        ]
294
          );
295
296
    my $smtp_server_to_delete =
297
      $builder->build_object( { class => 'Koha::SMTP::Servers' } );
298
    my $non_existent_id = $smtp_server_to_delete->id;
299
    $smtp_server_to_delete->delete;
300
301
    $t->put_ok(
302
        "//$userid:$password@/api/v1/config/smtp_servers/$non_existent_id" =>
303
          json => $smtp_server_with_updated_field )->status_is(404);
304
305
    # Wrong method (POST)
306
    $smtp_server_with_updated_field->{smtp_server_id} = 2;
307
308
    $t->post_ok(
309
        "//$userid:$password@/api/v1/config/smtp_servers/$smtp_server_id" =>
310
          json => $smtp_server_with_updated_field )->status_is(404);
311
312
    $schema->storage->txn_rollback;
313
};
314
315
subtest 'delete() tests' => sub {
316
317
    plan tests => 7;
318
319
    $schema->storage->txn_begin;
320
321
    my $librarian = $builder->build_object(
322
        {
323
            class => 'Koha::Patrons',
324
            value => { flags => 3**2 }    # parameters flag = 3
325
        }
326
    );
327
    my $password = 'thePassword123';
328
    $librarian->set_password( { password => $password, skip_validation => 1 } );
329
    my $userid = $librarian->userid;
330
331
    my $patron = $builder->build_object(
332
        {
333
            class => 'Koha::Patrons',
334
            value => { flags => 0 }
335
        }
336
    );
337
338
    $patron->set_password( { password => $password, skip_validation => 1 } );
339
    my $unauth_userid = $patron->userid;
340
341
    my $smtp_server_id =
342
      $builder->build_object( { class => 'Koha::SMTP::Servers' } )->id;
343
344
    # Unauthorized attempt to delete
345
    $t->delete_ok(
346
        "//$unauth_userid:$password@/api/v1/config/smtp_servers/$smtp_server_id"
347
    )->status_is(403);
348
349
    $t->delete_ok(
350
        "//$userid:$password@/api/v1/config/smtp_servers/$smtp_server_id")
351
      ->status_is( 204, 'SWAGGER3.2.4' )->content_is( '', 'SWAGGER3.3.4' );
352
353
    $t->delete_ok(
354
        "//$userid:$password@/api/v1/config/smtp_servers/$smtp_server_id")
355
      ->status_is(404);
356
357
    $schema->storage->txn_rollback;
358
};

Return to bug 22343