Skip to content

Commit

Permalink
add generate_opcode function, update tests
Browse files Browse the repository at this point in the history
  • Loading branch information
JGreenlee committed Jan 21, 2025
1 parent 888de57 commit ebdbf3a
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 4 deletions.
9 changes: 9 additions & 0 deletions src/emcommon/auth/opcode.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from __future__ import annotations # __: skip
import secrets # __: skip


Expand Down Expand Up @@ -25,3 +26,11 @@ def generate_random_string(length: int, charset=None):
# Python implementation
random_str += secrets.choice(charset) # __: skip
return random_str


def generate_opcode(prefix: str, program: str, subgroup: str | None, token_length: int):
opcode_parts = [prefix, program]
if subgroup:
opcode_parts.append(subgroup)
opcode_parts.append(generate_random_string(token_length))
return '_'.join(opcode_parts)
47 changes: 43 additions & 4 deletions test/auth/test_opcode.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@

@jest_test
def test_generate_random_string_alphanumeric():
random_strs = [emcao.generate_random_string(10) for _ in range(100)]
random_strs = [emcao.generate_random_string(12) for _ in range(100)]
expectEqual(len(random_strs), 100)
for random_string in random_strs:
expectEqual(len(random_string), 10)
expectEqual(len(random_string), 12)
expect(
all(char in emcao.ALPHANUMERIC for char in random_string)
)
Expand All @@ -16,13 +16,52 @@ def test_generate_random_string_alphanumeric():
@jest_test
def test_generate_random_string_vowels():
VOWELS = 'AEIOUYaeiouy'
random_strs = [emcao.generate_random_string(10, VOWELS) for _ in range(100)]
random_strs = [emcao.generate_random_string(8, VOWELS) for _ in range(100)]
expectEqual(len(random_strs), 100)
for random_string in random_strs:
expectEqual(len(random_string), 10)
expectEqual(len(random_string), 8)
expect(
all(char in VOWELS for char in random_string)
)


@jest_test
def test_generate_opcode_with_subgroup():
'''
Generate and validate 100 opcodes for fake study 'hogwarts-transit'
with subgroup 'gryffindor'
'''
opcodes = [
emcao.generate_opcode('nrelop', 'hogwarts-transit', 'gryffindor', 10)
for _ in range(100)
]
expectEqual(len(opcodes), 100)
for opcode in opcodes:
opcode_parts = opcode.split('_')
expect(len(opcode_parts) == 4)
expect(opcode_parts[0] == 'nrelop')
expect(opcode_parts[1] == 'hogwarts-transit')
expect(opcode_parts[2] == 'gryffindor')
expectEqual(len(opcode_parts[3]), 10)


@jest_test
def test_generate_opcode_without_subgroup():
'''
Generate and validate 100 opcodes for fake study 'coruscant-transit'
with no subgroup
'''
opcodes = [
emcao.generate_opcode('nrelop', 'coruscant-transit', None, 10)
for _ in range(100)
]
expectEqual(len(opcodes), 100)
for opcode in opcodes:
opcode_parts = opcode.split('_')
expect(len(opcode_parts) == 3)
expect(opcode_parts[0] == 'nrelop')
expect(opcode_parts[1] == 'coruscant-transit')
expectEqual(len(opcode_parts[2]), 10)


jest_describe('test_opcode')

0 comments on commit ebdbf3a

Please sign in to comment.