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

(-)a/Koha/REST/V1/Config/SMTP/DomainLimits.pm (+160 lines)
Line 0 Link Here
1
package Koha::REST::V1::Config::SMTP::DomainLimits;
2
3
# Copyright 2023 Rijksmuseum, Koha development team
4
#
5
# This file is part of Koha.
6
#
7
# Koha is free software; you can redistribute it and/or modify it
8
# under the terms of the GNU General Public License as published by
9
# the Free Software Foundation; either version 3 of the License, or
10
# (at your option) any later version.
11
#
12
# Koha is distributed in the hope that it will be useful, but
13
# WITHOUT ANY WARRANTY; without even the implied warranty of
14
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15
# GNU General Public License for more details.
16
#
17
# You should have received a copy of the GNU General Public License
18
# along with Koha; if not, see <http://www.gnu.org/licenses>.
19
20
use Modern::Perl;
21
use Mojo::Base 'Mojolicious::Controller';
22
use Scalar::Util qw( blessed );
23
use Try::Tiny qw( catch try );
24
25
use Koha::MailDomainLimits;
26
27
=head1 API
28
29
=head2 Methods
30
31
=head3 list
32
33
Controller method that handles listing domain limits
34
35
=cut
36
37
sub list {
38
    my $c = shift->openapi->valid_input or return;
39
    return try {
40
        my $limits = $c->objects->search( Koha::MailDomainLimits->search_with_group_domain );
41
        return $c->render( _render_helper( 200, undef, result => $limits ) );
42
    }
43
    catch {
44
        $c->unhandled_exception($_);
45
    };
46
}
47
48
=head3 get
49
50
Controller method that handles retrieving a single domain limit
51
52
=cut
53
54
sub get {
55
    my $c = shift->openapi->valid_input or return;
56
    return try {
57
        my $rec = Koha::MailDomainLimits->search_with_group_domain->find( $c->validation->param('domain_limit_id') );
58
        return $c->render( _render_helper( 404, undef, error => "Domain limit not found" ) ) if !$rec;
59
        return $c->render( _render_helper( 200, undef, result => $rec->to_api ) );
60
    }
61
    catch {
62
        $c->unhandled_exception($_);
63
    }
64
}
65
66
=head3 add
67
68
Controller method that handles adding a new domain limit
69
70
=cut
71
72
sub add {
73
    my $c = shift->openapi->valid_input or return;
74
    return try {
75
        my $limit = Koha::MailDomainLimit->new_from_api( $c->validation->param('body') );
76
        $limit->store->discard_changes;
77
        $c->res->headers->location( $c->req->url->to_string . '/' . $limit->id );
78
        return $c->render( _render_helper( 201, undef, result => $limit->to_api ) );
79
    }
80
    catch {
81
        if( blessed($_) ) {
82
            return $c->render( _render_helper( 409, $_ ) )
83
                if $_->isa('Koha::Exceptions::DomainLimit::NoSelfChaining')
84
                    || $_->isa('Koha::Exceptions::DomainLimit::MemberWithLimit')
85
                    || $_->isa('Koha::Exceptions::Object::DuplicateID');
86
            return $c->render( _render_helper( 400, $_, missing => $_->parameter ) )
87
                if $_->isa('Koha::Exceptions::DomainLimit::EmptyLimitData');
88
            return $c->render( _render_helper( 404, $_ ) )
89
                if $_->isa('Koha::Exceptions::Object::FKConstraint'); # on belongs_to
90
        }
91
        $c->unhandled_exception($_);
92
    };
93
}
94
95
=head3 update
96
97
Controller method that handles updating a domain limit
98
99
=cut
100
101
sub update {
102
    my $c = shift->openapi->valid_input or return;
103
    my $limit = Koha::MailDomainLimits->find( $c->validation->param('domain_limit_id') );
104
    return $c->render(  _render_helper( 404, undef, error => "Object not found" ) ) if !$limit;
105
    return try {
106
        $limit->set_from_api( $c->validation->param('body') );
107
        $limit->store->discard_changes;
108
        return $c->render( _render_helper( 200, undef, result => $limit->to_api ) );
109
    }
110
    catch {
111
        if( blessed($_) ) {
112
            return $c->render(_render_helper( 409, $_ ) )
113
                if $_->isa('Koha::Exceptions::DomainLimit::NoSelfChaining')
114
                    || $_->isa('Koha::Exceptions::DomainLimit::MemberWithLimit')
115
                    || $_->isa('Koha::Exceptions::Object::DuplicateID');
116
            return $c->render(_render_helper( 400, $_, missing => $_->parameter ) )
117
                if $_->isa('Koha::Exceptions::DomainLimit::EmptyLimitData');
118
            return $c->render(_render_helper( 404, $_ ) )
119
                if $_->isa('Koha::Exceptions::Object::FKConstraint'); # on belongs_to
120
        }
121
        $c->unhandled_exception($_);
122
    };
123
}
124
125
=head3 delete
126
127
Controller method that handles deleting a domain limit
128
129
=cut
130
131
sub delete {
132
    my $c = shift->openapi->valid_input or return;
133
    my $limit = Koha::MailDomainLimits->find( $c->validation->param('domain_limit_id') );
134
    return $c->render(  _render_helper( 404, undef, error => "Domain limit not found" ) ) if !$limit;
135
    return try {
136
        $limit->delete;
137
        return $c->render( _render_helper( 204, undef, result => q{} ) );
138
    }
139
    catch {
140
        $c->unhandled_exception($_);
141
    };
142
}
143
144
=head2 Internal routines
145
146
=cut
147
148
sub _render_helper {
149
    my ( $status, $exception, %result ) = @_; # %result can be: result => $result to pass only one result
150
    my $resp = { status => $status };
151
    my @error_message = $exception ? ( error => ( $exception->message || $exception->description ) ) : ();
152
    if( @error_message ) {
153
        $resp->{openapi} = { @error_message, %result };
154
    } elsif( keys %result ) {
155
        $resp->{openapi} = $result{result} // { %result };
156
    }
157
    return %$resp;
158
}
159
160
1;
(-)a/api/v1/swagger/definitions/smtp_domain_limits.yaml (+44 lines)
Line 0 Link Here
1
---
2
type: object
3
properties:
4
  domain_limit_id:
5
    description: Internally assigned domain limit identifier
6
    type: integer
7
    readOnly: true
8
  domain_name:
9
    description: Name of domain
10
    type: string
11
  domain_limit:
12
    description: Maximum number of messages per period
13
    type:
14
      - integer
15
      - "null"
16
  units:
17
    description: Number of units per period
18
    type:
19
      - integer
20
      - "null"
21
  unit_type:
22
    description: Unit of time for period
23
    type:
24
      - string
25
      - "null"
26
    enum:
27
      - minutes
28
      - hours
29
      - days
30
      - null
31
  belongs_to:
32
    description: Domain limit identifier of group domain
33
    type:
34
      - integer
35
      - "null"
36
  group_domain:
37
    description: Name of group domain (from self join)
38
    type:
39
      - string
40
      - "null"
41
    readOnly: true
42
additionalProperties: false
43
required:
44
  - domain_name
(-)a/api/v1/swagger/paths/smtp_domain_limits.yaml (+213 lines)
Line 0 Link Here
1
---
2
/smtp/domain_limits:
3
  get:
4
    x-mojo-to: Config::SMTP::DomainLimits#list
5
    operationId: listDomainLimits
6
    tags:
7
      - domain_limits
8
    summary: List mail domain limits
9
    produces:
10
      - application/json
11
    parameters:
12
      - $ref: ../swagger.yaml#/parameters/match
13
      - $ref: ../swagger.yaml#/parameters/order_by
14
      - $ref: ../swagger.yaml#/parameters/page
15
      - $ref: ../swagger.yaml#/parameters/per_page
16
      - $ref: ../swagger.yaml#/parameters/q_param
17
      - $ref: ../swagger.yaml#/parameters/q_body
18
      - $ref: ../swagger.yaml#/parameters/request_id_header
19
    responses:
20
      "200":
21
        description: A list of domain limits
22
        schema:
23
          type: array
24
          items:
25
            $ref: "../swagger.yaml#/definitions/smtp_domain_limits"
26
      "403":
27
        description: Access forbidden
28
        schema:
29
          $ref: "../swagger.yaml#/definitions/error"
30
      "500":
31
        description: |
32
          Internal server error. Possible `error_code` attribute values:
33
34
          * `internal_server_error`
35
        schema:
36
          $ref: "../swagger.yaml#/definitions/error"
37
      "503":
38
        description: Under maintenance
39
        schema:
40
          $ref: "../swagger.yaml#/definitions/error"
41
    x-koha-authorization:
42
      permissions:
43
        parameters: manage_smtp_servers
44
  post:
45
    x-mojo-to: Config::SMTP::DomainLimits#add
46
    operationId: addDomainLimit
47
    tags:
48
      - domain_limits
49
    summary: Add a new domain limit
50
    parameters:
51
      - name: body
52
        in: body
53
        description: A JSON object representing a new domain limit
54
        required: true
55
        schema:
56
          $ref: "../swagger.yaml#/definitions/smtp_domain_limits"
57
    consumes:
58
      - application/json
59
    produces:
60
      - application/json
61
    responses:
62
      "201":
63
        description: Created a domain limit
64
        schema:
65
          $ref: "../swagger.yaml#/definitions/smtp_domain_limits"
66
      "400":
67
        description: Bad Request
68
        schema:
69
          $ref: ../swagger.yaml#/definitions/error
70
      "403":
71
        description: Access forbidden
72
        schema:
73
          $ref: ../swagger.yaml#/definitions/error
74
      "404":
75
        description: Object not found
76
        schema:
77
          $ref: ../swagger.yaml#/definitions/error
78
      "409":
79
        description: Conflict in creating resource
80
        schema:
81
          $ref: "../swagger.yaml#/definitions/error"
82
      "500":
83
        description: |
84
          Internal server error. Possible `error_code` attribute values:
85
          * `internal_server_error`
86
        schema:
87
          $ref: ../swagger.yaml#/definitions/error
88
      "503":
89
        description: Under maintenance
90
        schema:
91
          $ref: ../swagger.yaml#/definitions/error
92
    x-koha-authorization:
93
      permissions:
94
        parameters: manage_smtp_servers
95
"/smtp/domain_limits/{domain_limit_id}":
96
  get:
97
    x-mojo-to: Config::SMTP::DomainLimits#get
98
    operationId: getDomainLimit
99
    tags:
100
      - domain_limits
101
    summary: Get domain limit
102
    parameters:
103
      - $ref: ../swagger.yaml#/parameters/domain_limit_id_pp
104
    produces:
105
      - application/json
106
    responses:
107
      "200":
108
        description: A domain limit
109
        schema:
110
          $ref: ../swagger.yaml#/definitions/smtp_domain_limits
111
      "404":
112
        description: Object not found
113
        schema:
114
          $ref: ../swagger.yaml#/definitions/error
115
      "500":
116
        description: |
117
          Internal server error. Possible `error_code` attribute values:
118
          * `internal_server_error`
119
        schema:
120
          $ref: ../swagger.yaml#/definitions/error
121
      "503":
122
        description: Under maintenance
123
        schema:
124
          $ref: ../swagger.yaml#/definitions/error
125
    x-koha-authorization:
126
      permissions:
127
        parameters: manage_smtp_servers
128
  put:
129
    x-mojo-to: Config::SMTP::DomainLimits#update
130
    operationId: updateDomainLimit
131
    tags:
132
      - domain_limits
133
    summary: Update a domain limit
134
    parameters:
135
      - $ref: ../swagger.yaml#/parameters/domain_limit_id_pp
136
      - name: body
137
        in: body
138
        description: A JSON object representing a new domain limit
139
        required: true
140
        schema:
141
          $ref: "../swagger.yaml#/definitions/smtp_domain_limits"
142
    produces:
143
      - application/json
144
    responses:
145
      "200":
146
        description: Updated domain limit
147
        schema:
148
          $ref: ../swagger.yaml#/definitions/smtp_domain_limits
149
      "400":
150
        description: Bad Request
151
        schema:
152
          $ref: ../swagger.yaml#/definitions/error
153
      "403":
154
        description: Access forbidden
155
        schema:
156
          $ref: ../swagger.yaml#/definitions/error
157
      "404":
158
        description: Object not found
159
        schema:
160
          $ref: ../swagger.yaml#/definitions/error
161
      "409":
162
        description: Conflict in creating resource
163
        schema:
164
          $ref: "../swagger.yaml#/definitions/error"
165
      "500":
166
        description: |
167
          Internal server error. Possible `error_code` attribute values:
168
          * `internal_server_error`
169
        schema:
170
          $ref: ../swagger.yaml#/definitions/error
171
      "503":
172
        description: Under maintenance
173
        schema:
174
          $ref: ../swagger.yaml#/definitions/error
175
    x-koha-authorization:
176
      permissions:
177
        parameters: manage_smtp_servers
178
  delete:
179
    x-mojo-to: Config::SMTP::DomainLimits#delete
180
    operationId: delDomainLimit
181
    tags:
182
      - domain_limits
183
    summary: Delete domain limit
184
    parameters:
185
      - $ref: ../swagger.yaml#/parameters/domain_limit_id_pp
186
    produces:
187
      - application/json
188
    responses:
189
      "204":
190
        description: Domain limit deleted
191
      "401":
192
        description: Authentication required
193
        schema:
194
          $ref: ../swagger.yaml#/definitions/error
195
      "403":
196
        description: Access forbidden
197
        schema:
198
          $ref: ../swagger.yaml#/definitions/error
199
      "404":
200
        description: Not found
201
        schema:
202
          $ref: ../swagger.yaml#/definitions/error
203
      "500":
204
        description: |
205
          Internal server error. Possible `error_code` attribute values:
206
          * `internal_server_error`
207
      "503":
208
        description: Under maintenance
209
        schema:
210
          $ref: ../swagger.yaml#/definitions/error
211
    x-koha-authorization:
212
      permissions:
213
        parameters: manage_smtp_servers
(-)a/api/v1/swagger/swagger.yaml (+15 lines)
Lines 102-107 definitions: Link Here
102
    $ref: ./definitions/search_filter.yaml
102
    $ref: ./definitions/search_filter.yaml
103
  smtp_server:
103
  smtp_server:
104
    $ref: ./definitions/smtp_server.yaml
104
    $ref: ./definitions/smtp_server.yaml
105
  smtp_domain_limits:
106
    $ref: ./definitions/smtp_domain_limits.yaml
105
  suggestion:
107
  suggestion:
106
    $ref: ./definitions/suggestion.yaml
108
    $ref: ./definitions/suggestion.yaml
107
  ticket:
109
  ticket:
Lines 361-366 paths: Link Here
361
    $ref: "./paths/return_claims.yaml#/~1return_claims~1{claim_id}~1resolve"
363
    $ref: "./paths/return_claims.yaml#/~1return_claims~1{claim_id}~1resolve"
362
  "/rotas/{rota_id}/stages/{stage_id}/position":
364
  "/rotas/{rota_id}/stages/{stage_id}/position":
363
    $ref: "./paths/rotas.yaml#/~1rotas~1{rota_id}~1stages~1{stage_id}~1position"
365
    $ref: "./paths/rotas.yaml#/~1rotas~1{rota_id}~1stages~1{stage_id}~1position"
366
  /smtp/domain_limits:
367
    $ref: ./paths/smtp_domain_limits.yaml#/~1smtp~1domain_limits
368
  "/smtp/domain_limits/{domain_limit_id}":
369
    $ref: "./paths/smtp_domain_limits.yaml#/~1smtp~1domain_limits~1{domain_limit_id}"
364
  /suggestions:
370
  /suggestions:
365
    $ref: ./paths/suggestions.yaml#/~1suggestions
371
    $ref: ./paths/suggestions.yaml#/~1suggestions
366
  "/suggestions/{suggestion_id}":
372
  "/suggestions/{suggestion_id}":
Lines 491-496 parameters: Link Here
491
    name: club_id
497
    name: club_id
492
    required: true
498
    required: true
493
    type: integer
499
    type: integer
500
  domain_limit_id_pp:
501
    description: SMTP domain limit indentifier
502
    in: path
503
    name: domain_limit_id
504
    required: true
505
    type: integer
494
  eholdings_title_id_pp:
506
  eholdings_title_id_pp:
495
    description: Title internal identifier
507
    description: Title internal identifier
496
    in: path
508
    in: path
Lines 899-904 tags: Link Here
899
  - description: "Manage SMTP servers configurations\n"
911
  - description: "Manage SMTP servers configurations\n"
900
    name: smtp_servers
912
    name: smtp_servers
901
    x-displayName: SMTP servers
913
    x-displayName: SMTP servers
914
  - description: "Manage SMTP domain limits\n"
915
    name: domain_limits
916
    x-displayName: SMTP domain limits
902
  - description: "Manage tickets\n"
917
  - description: "Manage tickets\n"
903
    name: tickets
918
    name: tickets
904
    x-displayName: Tickets
919
    x-displayName: Tickets
(-)a/t/db_dependent/api/v1/domain_limits.t (-1 / +86 lines)
Line 0 Link Here
0
- 
1
#!/usr/bin/env perl
2
3
# Copyright 2023 Rijksmuseum, Koha development team
4
#
5
# This file is part of Koha.
6
#
7
# Koha is free software; you can redistribute it and/or modify it
8
# under the terms of the GNU General Public License as published by
9
# the Free Software Foundation; either version 3 of the License, or
10
# (at your option) any later version.
11
#
12
# Koha is distributed in the hope that it will be useful, but
13
# WITHOUT ANY WARRANTY; without even the implied warranty of
14
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15
# GNU General Public License for more details.
16
#
17
# You should have received a copy of the GNU General Public License
18
# along with Koha; if not, see <http://www.gnu.org/licenses>.
19
20
use Modern::Perl;
21
use Test::More tests => 1;
22
use Test::Mojo;
23
24
use t::lib::TestBuilder;
25
use t::lib::Mocks;
26
27
use Koha::Database;
28
use Koha::MailDomainLimits;
29
30
my $schema  = Koha::Database->new->schema;
31
my $builder = t::lib::TestBuilder->new;
32
33
my $t = Test::Mojo->new('Koha::REST::V1');
34
t::lib::Mocks::mock_preference( 'RESTBasicAuth', 1 );
35
36
subtest 'Few CRUD tests' => sub {
37
    plan tests => 28;
38
    $schema->storage->txn_begin;
39
40
    Koha::MailDomainLimits->delete;
41
    my $user     = $builder->build_object({ class => 'Koha::Patrons' });
42
    my $userid   = $user->userid;
43
    my $password = '2345';
44
    $user->set_password( { password => $password, skip_validation => 1 } );
45
46
    # Test GET ALL, 403 Unauthorized
47
    $t->get_ok("//$userid:$password@/api/v1/smtp/domain_limits")->status_is(403);
48
    # Change flags
49
    $user->flags(8)->store;
50
    $t->get_ok("//$userid:$password@/api/v1/smtp/domain_limits")->status_is(200)->json_is( [] );
51
52
    # Add with POST
53
    my $data = { domain_name => 'test.nl', domain_limit => 1, units => 2, unit_type => 'minutes' };
54
    $t->post_ok( "//$userid:$password@/api/v1/smtp/domain_limits" => json => $data )->status_is(201);
55
    is( Koha::MailDomainLimits->count, 1, 'Added record' );
56
    my $limit = Koha::MailDomainLimits->search->next;
57
    my $limit_id = $limit->id;
58
    is( $limit->domain_name, 'test.nl', 'Check a field' );
59
    # Add member
60
    $t->post_ok( "//$userid:$password@/api/v1/smtp/domain_limits" => json => { domain_name => 'test.be', belongs_to => $limit_id } )
61
    ->status_is(201);
62
63
    # Single GET, check joined column for member just added
64
    $t->get_ok("//$userid:$password@/api/v1/smtp/domain_limits/0")->status_is(404);
65
    my $member_id = $limit_id + 1;
66
    $t->get_ok("//$userid:$password@/api/v1/smtp/domain_limits/$member_id")->status_is(200)->json_has('/group_domain', 'test.nl');
67
68
    # Modify with PUT
69
    $data->{units} = 3;
70
    $t->put_ok( "//$userid:$password@/api/v1/smtp/domain_limits/$limit_id" => json => $data )->status_is(200);
71
    $limit->discard_changes;
72
    is( $limit->units, 3, 'PUT changed a field' );
73
    # Try to set belongs_to, triggers exception
74
    $data->{belongs_to} = $limit->id;
75
    $t->put_ok( "//$userid:$password@/api/v1/smtp/domain_limits/$limit_id" => json => $data )->status_is(409);
76
77
    # Trigger 409 for duplicate domain_name
78
    $t->post_ok( "//$userid:$password@/api/v1/smtp/domain_limits" => json => $data )->status_is(409);
79
80
    # Try DELETE
81
    $t->delete_ok( "//$userid:$password@/api/v1/smtp/domain_limits/$limit_id" )->status_is(204);
82
    is( Koha::MailDomainLimits->count, 0, 'Removed record' );
83
    $t->delete_ok( "//$userid:$password@/api/v1/smtp/domain_limits/$limit_id" )->status_is(404);
84
85
    $schema->storage->txn_rollback;
86
};

Return to bug 33537