Repository: freedomofpress/securedrop

Issue:
[2.3.0] `/lookup` fails to render on fresh install due to `min_length` value in template being set to None
## Description

On a fresh install, the initial_message_min_len field in instance_config is NULL, causing checks for equality with integers to fail.

## Steps to Reproduce

- Install 2.3.0-rc1 packages in production or staging
- visit the SI without updating any config settings in the JI

## Expected Behavior

- SI renders correctly

## Actual Behavior

500 errors, with errors like "TypeError: '>' not supported between instances of 'NoneType' and 'int'" in the source logs, if enabled.
Please provide screenshots where appropriate.

## Comments




Discussion:

Resolving patch:
diff --git a/securedrop/alembic/versions/55f26cd22043_remove_partial_index_on_valid_until_set_.py b/securedrop/alembic/versions/d9d36b6f4d1e_remove_partial_index_on_valid_until_set_.py
similarity index 70%
rename from securedrop/alembic/versions/55f26cd22043_remove_partial_index_on_valid_until_set_.py
rename to securedrop/alembic/versions/d9d36b6f4d1e_remove_partial_index_on_valid_until_set_.py
--- a/securedrop/alembic/versions/55f26cd22043_remove_partial_index_on_valid_until_set_.py
+++ b/securedrop/alembic/versions/d9d36b6f4d1e_remove_partial_index_on_valid_until_set_.py
@@ -1,8 +1,8 @@
 """remove partial_index on valid_until, set it to nullable=false, add message filter fields
 
-Revision ID: 55f26cd22043
+Revision ID: d9d36b6f4d1e
 Revises: 2e24fc7536e8
-Create Date: 2022-03-03 22:40:36.149638
+Create Date: 2022-03-16 17:31:28.408112
 
 """
 from alembic import op
@@ -11,13 +11,14 @@
 from datetime import datetime
 
 # revision identifiers, used by Alembic.
-revision = '55f26cd22043'
+revision = 'd9d36b6f4d1e'
 down_revision = '2e24fc7536e8'
 branch_labels = None
 depends_on = None
 
 
 def upgrade():
+    # ### commands auto generated by Alembic - please adjust! ###
     # remove the old partial index on valid_until, as alembic can't handle it.
     op.execute(sa.text('DROP INDEX IF EXISTS ix_one_active_instance_config'))
 
@@ -27,25 +28,38 @@ def upgrade():
         'UPDATE OR IGNORE instance_config SET '
         'valid_until=:epoch WHERE valid_until IS NULL;'
     ).bindparams(epoch=datetime.fromtimestamp(0)))
+
+    # add new columns with default values
     with op.batch_alter_table('instance_config', schema=None) as batch_op:
+        batch_op.add_column(sa.Column('initial_message_min_len',
+                                      sa.Integer(),
+                                      nullable=False,
+                                      server_default="0"))
+        batch_op.add_column(sa.Column('reject_message_with_codename',
+                                      sa.Boolean(),
+                                      nullable=False,
+                                      server_default="0"))
         batch_op.alter_column('valid_until',
-                              existing_type=sa.DATETIME(),
-                              nullable=False)
-        batch_op.add_column(sa.Column('initial_message_min_len', sa.Integer(), nullable=True))
-        batch_op.add_column(sa.Column('reject_message_with_codename', sa.Boolean(), nullable=True))
+               existing_type=sa.DATETIME(),
+               nullable=False)
 
     # remove the old partial index *again* in case the batch op recreated it.
     op.execute(sa.text('DROP INDEX IF EXISTS ix_one_active_instance_config'))
 
 
+    # ### end Alembic commands ###
+
+
 def downgrade():
+    # ### commands auto generated by Alembic - please adjust! ###
     with op.batch_alter_table('instance_config', schema=None) as batch_op:
         batch_op.alter_column('valid_until',
-                              existing_type=sa.DATETIME(),
-                              nullable=True)
+               existing_type=sa.DATETIME(),
+               nullable=True)
         batch_op.drop_column('reject_message_with_codename')
         batch_op.drop_column('initial_message_min_len')
 
+
     # valid_until is nullable again, set entries with unix epoch datetime value to NULL
     op.execute(sa.text(
         'UPDATE OR IGNORE instance_config SET '
@@ -55,3 +69,6 @@ def downgrade():
     # manually restore the partial index
     op.execute(sa.text('CREATE UNIQUE INDEX IF NOT EXISTS ix_one_active_instance_config ON '
                        'instance_config (valid_until IS NULL) WHERE valid_until IS NULL'))
+
+
+    # ### end Alembic commands ###
diff --git a/securedrop/models.py b/securedrop/models.py
--- a/securedrop/models.py
+++ b/securedrop/models.py
@@ -944,8 +944,9 @@ class InstanceConfig(db.Model):
                          nullable=False, unique=True)
     allow_document_uploads = Column(Boolean, default=True)
     organization_name = Column(String(255), nullable=True, default="SecureDrop")
-    initial_message_min_len = Column(Integer, default=0)
-    reject_message_with_codename = Column(Boolean, default=False)
+    initial_message_min_len = Column(Integer, nullable=False, default=0, server_default="0")
+    reject_message_with_codename = Column(Boolean, nullable=False,
+                                          default=False, server_default="0")
 
     # Columns not listed here will be included by InstanceConfig.copy() when
     # updating the configuration.


Test patch:
diff --git a/securedrop/tests/migrations/migration_55f26cd22043.py b/securedrop/tests/migrations/migration_d9d36b6f4d1e.py
similarity index 100%
rename from securedrop/tests/migrations/migration_55f26cd22043.py
rename to securedrop/tests/migrations/migration_d9d36b6f4d1e.py