Skip to content

Commit 3fd0d87

Browse files
committed
Introduce PhraseIdentifier::PATTERN
The `PhraseIdentifier.generate` method is used to generate an identifier for projects created by teachers and students. However, public projects, e.g. those created via the `GithubWebhooksController#github_push` [1] action and those created via the (possibly defunct) `projects:create_all` rake task [2], have identifiers specified in their project config via `ProjectImporter` [3]. While there is no *explicit* validation to enforce it, the identifiers for these public projects seems to follow the pattern described by the regular expression I have defined in `PhraseIdentifier::PATTERN`, i.e. kebab-case with digits allowed, at least in the 2nd word [4]. The pattern could probably be stricter, but since I don't have access to the production data, it seems safer for it to be more relaxed. We want admin users in experience-cs to be able to create public scratch projects via the editor-api API and I think it makes sense to restrict them to using identifiers following a similar pattern. I plan to add some validation to enforce that and so we can provide feedback to the admin user if they choose an identifier with the wrong format. I'll be able to use this `PhraseIdentifier::PATTERN` in that validation. [1]: https://github.com/RaspberryPiFoundation/editor-api/blob/f348dfc92fdd8b19fbfd5434e7751340f33b4093/app/controllers/github_webhooks_controller.rb#L6-L8 [2]: https://github.com/RaspberryPiFoundation/editor-api/blob/f348dfc92fdd8b19fbfd5434e7751340f33b4093/lib/tasks/projects.rake#L4-L7 [3]: https://github.com/RaspberryPiFoundation/editor-api/blob/f348dfc92fdd8b19fbfd5434e7751340f33b4093/lib/project_importer.rb [4]: https://github.com/RaspberryPiFoundation/editor-api/blob/f348dfc92fdd8b19fbfd5434e7751340f33b4093/lib/tasks/project_components/top_5_emoji_list/project_config.yml#L2
1 parent 57e5349 commit 3fd0d87

File tree

2 files changed

+28
-0
lines changed

2 files changed

+28
-0
lines changed

lib/phrase_identifier.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
# frozen_string_literal: true
22

33
class PhraseIdentifier
4+
PATTERN = /\A[a-z0-9]+(-[a-z0-9]+)*\z/
5+
46
class Error < RuntimeError
57
end
68

spec/lib/phrase_identifier_spec.rb

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,24 @@
44
require 'phrase_identifier'
55

66
RSpec.describe PhraseIdentifier do
7+
describe 'PATTERN' do
8+
subject(:pattern) { described_class::PATTERN }
9+
10+
it { is_expected.to match('abc-def-ghi') }
11+
it { is_expected.to match('123-456-789') }
12+
it { is_expected.to match('a2c-d5f-g8i') }
13+
14+
it { is_expected.not_to match('Abc-def-ghi') }
15+
it { is_expected.not_to match('Abc-def-GHI') }
16+
it { is_expected.not_to match('abc--def-ghi') }
17+
it { is_expected.not_to match('-abc-def-ghi') }
18+
it { is_expected.not_to match('abc-def-ghi-') }
19+
it { is_expected.not_to match('abc def-ghi') }
20+
it { is_expected.not_to match(' abc-def-ghi') }
21+
it { is_expected.not_to match('abc-def-ghi ') }
22+
it { is_expected.not_to match('abc_def_ghi') }
23+
end
24+
725
describe '#generate' do
826
subject(:generate) { described_class.generate }
927

@@ -18,6 +36,14 @@
1836
it { is_expected.to match phrase_regex }
1937
end
2038

39+
context 'when using the default words.txt file' do
40+
it 'returns identifiers conforming to the expected pattern' do
41+
10.times do
42+
expect(generate).to match(described_class::PATTERN)
43+
end
44+
end
45+
end
46+
2147
context 'when there are no available combinations' do
2248
let(:identifier) { Faker::Verb.base }
2349

0 commit comments

Comments
 (0)