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 (+57 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
    "host": {
14
      "type": "string",
15
      "description": "SMTP host name"
16
    },
17
    "port": {
18
      "type": "integer",
19
      "description": "TCP port number"
20
    },
21
    "timeout": {
22
      "type": "integer",
23
      "description": "Maximum time in seconds to wait for server"
24
    },
25
    "ssl_mode": {
26
      "type": "string",
27
      "enum": [
28
        "disabled",
29
        "ssl",
30
        "starttls"
31
      ],
32
      "description": "If SSL/TLS will be used"
33
    },
34
    "user_name": {
35
      "type": [
36
        "string",
37
        "null"
38
      ],
39
      "description": "The user name to use for authentication (optional)"
40
    },
41
    "password": {
42
      "type": [
43
        "string",
44
        "null"
45
      ],
46
      "description": "The password to use for authentication (optional)"
47
    },
48
    "debug": {
49
      "type": "boolean",
50
      "description": "If the SMTP connection is set to debug mode"
51
    }
52
  },
53
  "additionalProperties": false,
54
  "required": [
55
    "name"
56
  ]
57
}
(-)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 (+316 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": "manage_smtp_servers"
68
        }
69
      }
70
    },
71
    "post": {
72
      "x-mojo-to": "Config::SMTP::Servers#add",
73
      "operationId": "addSMTPServer",
74
      "tags": [
75
        "config",
76
        "smtp"
77
      ],
78
      "parameters": [
79
        {
80
          "name": "body",
81
          "in": "body",
82
          "description": "A JSON object representing a new SMTP server configuration",
83
          "required": true,
84
          "schema": {
85
            "$ref": "../definitions.json#/smtp_server"
86
          }
87
        }
88
      ],
89
      "produces": [
90
        "application/json"
91
      ],
92
      "responses": {
93
        "201": {
94
          "description": "An SMTP server object",
95
          "schema": {
96
            "$ref": "../definitions.json#/smtp_server"
97
          }
98
        },
99
        "401": {
100
          "description": "Authentication required",
101
          "schema": {
102
            "$ref": "../definitions.json#/error"
103
          }
104
        },
105
        "403": {
106
          "description": "Access forbidden",
107
          "schema": {
108
            "$ref": "../definitions.json#/error"
109
          }
110
        },
111
        "409": {
112
          "description": "Conflict in creating resource",
113
          "schema": {
114
            "$ref": "../definitions.json#/error"
115
          }
116
        },
117
        "500": {
118
          "description": "Internal error",
119
          "schema": {
120
            "$ref": "../definitions.json#/error"
121
          }
122
        },
123
        "503": {
124
          "description": "Under maintenance",
125
          "schema": {
126
            "$ref": "../definitions.json#/error"
127
          }
128
        }
129
      },
130
      "x-koha-authorization": {
131
        "permissions": {
132
          "parameters": "manage_smtp_servers"
133
        }
134
      }
135
    }
136
  },
137
  "/config/smtp_servers/{smtp_server_id}": {
138
    "get": {
139
      "x-mojo-to": "Config::SMTP::Servers#get",
140
      "operationId": "getSMTPServer",
141
      "tags": [
142
        "config",
143
        "smtp"
144
      ],
145
      "parameters": [
146
        {
147
          "$ref": "../parameters.json#/smtp_server_id_pp"
148
        }
149
      ],
150
      "produces": [
151
        "application/json"
152
      ],
153
      "responses": {
154
        "200": {
155
          "description": "An SMTP server object",
156
          "schema": {
157
            "$ref": "../definitions.json#/smtp_server"
158
          }
159
        },
160
        "404": {
161
          "description": "Object not found",
162
          "schema": {
163
            "$ref": "../definitions.json#/error"
164
          }
165
        },
166
        "409": {
167
          "description": "Conflict updating resource",
168
          "schema": {
169
            "$ref": "../definitions.json#/error"
170
          }
171
        },
172
        "500": {
173
          "description": "Internal error",
174
          "schema": {
175
            "$ref": "../definitions.json#/error"
176
          }
177
        },
178
        "503": {
179
          "description": "Under maintenance",
180
          "schema": {
181
            "$ref": "../definitions.json#/error"
182
          }
183
        }
184
      },
185
      "x-koha-authorization": {
186
        "permissions": {
187
          "parameters": "manage_smtp_servers"
188
        }
189
      }
190
    },
191
    "put": {
192
      "x-mojo-to": "Config::SMTP::Servers#update",
193
      "operationId": "updateSMTPServer",
194
      "tags": [
195
        "config",
196
        "smtp"
197
      ],
198
      "parameters": [
199
        {
200
          "$ref": "../parameters.json#/smtp_server_id_pp"
201
        },
202
        {
203
          "name": "body",
204
          "in": "body",
205
          "description": "An SMTP server object",
206
          "required": true,
207
          "schema": {
208
            "$ref": "../definitions.json#/smtp_server"
209
          }
210
        }
211
      ],
212
      "produces": [
213
        "application/json"
214
      ],
215
      "responses": {
216
        "200": {
217
          "description": "An SMTP server object",
218
          "schema": {
219
            "$ref": "../definitions.json#/smtp_server"
220
          }
221
        },
222
        "401": {
223
          "description": "Authentication required",
224
          "schema": {
225
            "$ref": "../definitions.json#/error"
226
          }
227
        },
228
        "403": {
229
          "description": "Access forbidden",
230
          "schema": {
231
            "$ref": "../definitions.json#/error"
232
          }
233
        },
234
        "404": {
235
          "description": "Object not found",
236
          "schema": {
237
            "$ref": "../definitions.json#/error"
238
          }
239
        },
240
        "500": {
241
          "description": "Internal error",
242
          "schema": {
243
            "$ref": "../definitions.json#/error"
244
          }
245
        },
246
        "503": {
247
          "description": "Under maintenance",
248
          "schema": {
249
            "$ref": "../definitions.json#/error"
250
          }
251
        }
252
      },
253
      "x-koha-authorization": {
254
        "permissions": {
255
          "parameters": "manage_smtp_servers"
256
        }
257
      }
258
    },
259
    "delete": {
260
      "x-mojo-to": "Config::SMTP::Servers#delete",
261
      "operationId": "deleteSMTPServer",
262
      "tags": [
263
        "config",
264
        "smtp"
265
      ],
266
      "parameters": [
267
        {
268
          "$ref": "../parameters.json#/smtp_server_id_pp"
269
        }
270
      ],
271
      "produces": [
272
        "application/json"
273
      ],
274
      "responses": {
275
        "204": {
276
          "description": "SMTP server deleted"
277
        },
278
        "401": {
279
          "description": "Authentication required",
280
          "schema": {
281
            "$ref": "../definitions.json#/error"
282
          }
283
        },
284
        "403": {
285
          "description": "Access forbidden",
286
          "schema": {
287
            "$ref": "../definitions.json#/error"
288
          }
289
        },
290
        "404": {
291
          "description": "Object not found",
292
          "schema": {
293
            "$ref": "../definitions.json#/error"
294
          }
295
        },
296
        "500": {
297
          "description": "Internal error",
298
          "schema": {
299
            "$ref": "../definitions.json#/error"
300
          }
301
        },
302
        "503": {
303
          "description": "Under maintenance",
304
          "schema": {
305
            "$ref": "../definitions.json#/error"
306
          }
307
        }
308
      },
309
      "x-koha-authorization": {
310
        "permissions": {
311
          "parameters": "manage_smtp_servers"
312
        }
313
      }
314
    }
315
  }
316
}
(-)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