Code Duplication    Length = 158-160 lines in 2 locations

backend/tracim_backend/tests/library/test_content_api.py 2 locations

@@ 3012-3171 (lines=160) @@
3009
        assert content3.label == 'index'
3010
        assert content2_nb_rev == len(content3.revisions)
3011
3012
    def test_archive_unarchive(self):
3013
        uapi = UserApi(
3014
            session=self.session,
3015
            config=self.app_config,
3016
            current_user=None,
3017
        )
3018
        group_api = GroupApi(
3019
            current_user=None,
3020
            session=self.session,
3021
            config=self.app_config,
3022
        )
3023
        groups = [group_api.get_one(Group.TIM_USER),
3024
                  group_api.get_one(Group.TIM_MANAGER),
3025
                  group_api.get_one(Group.TIM_ADMIN)]
3026
3027
        user1 = uapi.create_minimal_user(
3028
            email='this.is@user',
3029
            groups=groups,
3030
            save_now=True
3031
        )
3032
        u1id = user1.user_id
3033
3034
        workspace_api = WorkspaceApi(
3035
            current_user=user1,
3036
            session=self.session,
3037
            config=self.app_config,
3038
        )
3039
        workspace = workspace_api.create_workspace(
3040
            'test workspace',
3041
            save_now=True
3042
        )
3043
        wid = workspace.workspace_id
3044
3045
        user2 = uapi.create_minimal_user('[email protected]')
3046
        uapi.save(user2)
3047
3048
        RoleApi(
3049
            current_user=user1,
3050
            session=self.session,
3051
            config=self.app_config,
3052
        ).create_one(
3053
            user2,
3054
            workspace,
3055
            UserRoleInWorkspace.CONTENT_MANAGER,
3056
            with_notif=True,
3057
            flush=True
3058
        )
3059
3060
        # show archived is used at the top end of the test
3061
        api = ContentApi(
3062
            current_user=user1,
3063
            session=self.session,
3064
            show_archived=True,
3065
            config=self.app_config,
3066
        )
3067
        p = api.create(
3068
            content_type_slug=content_type_list.File.slug,
3069
            workspace=workspace,
3070
            parent=None,
3071
            label='this_is_a_page',
3072
            do_save=True
3073
        )
3074
3075
        u1id = user1.user_id
3076
        u2id = user2.user_id
3077
        pcid = p.content_id
3078
        poid = p.owner_id
3079
3080
        transaction.commit()
3081
3082
        ####
3083
3084
        # refresh after commit
3085
        user1 = UserApi(
3086
            current_user=None,
3087
            config=self.app_config,
3088
            session=self.session
3089
        ).get_one(u1id)
3090
        workspace = WorkspaceApi(
3091
            current_user=user1,
3092
            session=self.session,
3093
            config=self.app_config,
3094
        ).get_one(wid)
3095
3096
        content = api.get_one(pcid, content_type_list.Any_SLUG, workspace)
3097
        eq_(u1id, content.owner_id)
3098
        eq_(poid, content.owner_id)
3099
3100
        u2api = UserApi(
3101
            session=self.session,
3102
            config=self.app_config,
3103
            current_user=None,
3104
        )
3105
        u2 = u2api.get_one(u2id)
3106
        api2 = ContentApi(
3107
            current_user=u2,
3108
            session=self.session,
3109
            config=self.app_config,
3110
            show_archived=True,
3111
        )
3112
        content2 = api2.get_one(pcid, content_type_list.Any_SLUG, workspace)
3113
        with new_revision(
3114
                session=self.session,
3115
                tm=transaction.manager,
3116
                content=content2,
3117
        ):
3118
            api2.archive(content2)
3119
        api2.save(content2)
3120
        transaction.commit()
3121
3122
        # refresh after commit
3123
        user1 = UserApi(
3124
            current_user=None,
3125
            session=self.session,
3126
            config=self.app_config,
3127
        ).get_one(u1id)
3128
        workspace = WorkspaceApi(
3129
            current_user=user1,
3130
            session=self.session,
3131
            config=self.app_config,
3132
        ).get_one(wid)
3133
        u2 = UserApi(
3134
            current_user=None,
3135
            session=self.session,
3136
            config=self.app_config,
3137
        ).get_one(u2id)
3138
        api = ContentApi(
3139
            current_user=user1,
3140
            session=self.session,
3141
            config=self.app_config,
3142
            show_archived=True,
3143
        )
3144
        api2 = ContentApi(
3145
            current_user=u2,
3146
            session=self.session,
3147
            config=self.app_config,
3148
            show_archived=True,
3149
        )
3150
3151
        updated = api2.get_one(pcid, content_type_list.Any_SLUG, workspace)
3152
        eq_(u2id, updated.owner_id,
3153
            'the owner id should be {} (found {})'.format(u2id,
3154
                                                          updated.owner_id))
3155
        eq_(True, updated.is_archived)
3156
        eq_(ActionDescription.ARCHIVING, updated.revision_type)
3157
3158
        ####
3159
3160
        updated2 = api.get_one(pcid, content_type_list.Any_SLUG, workspace)
3161
        with new_revision(
3162
            session=self.session,
3163
            tm=transaction.manager,
3164
            content=updated,
3165
3166
        ):
3167
            api.unarchive(updated)
3168
        api.save(updated2)
3169
        eq_(False, updated2.is_archived)
3170
        eq_(ActionDescription.UNARCHIVING, updated2.revision_type)
3171
        eq_(u1id, updated2.owner_id)
3172
3173
    def test_delete_undelete(self):
3174
        uapi = UserApi(
@@ 3173-3330 (lines=158) @@
3170
        eq_(ActionDescription.UNARCHIVING, updated2.revision_type)
3171
        eq_(u1id, updated2.owner_id)
3172
3173
    def test_delete_undelete(self):
3174
        uapi = UserApi(
3175
            session=self.session,
3176
            config=self.app_config,
3177
            current_user=None,
3178
        )
3179
        group_api = GroupApi(
3180
            current_user=None,
3181
            session=self.session,
3182
            config=self.app_config,
3183
        )
3184
        groups = [group_api.get_one(Group.TIM_USER),
3185
                  group_api.get_one(Group.TIM_MANAGER),
3186
                  group_api.get_one(Group.TIM_ADMIN)]
3187
3188
        user1 = uapi.create_minimal_user(
3189
            email='this.is@user',
3190
            groups=groups,
3191
            save_now=True
3192
        )
3193
        u1id = user1.user_id
3194
3195
        workspace_api = WorkspaceApi(
3196
            current_user=user1,
3197
            session=self.session,
3198
            config=self.app_config,
3199
        )
3200
        workspace = workspace_api.create_workspace(
3201
            'test workspace',
3202
            save_now=True
3203
        )
3204
        wid = workspace.workspace_id
3205
3206
        user2 = uapi.create_minimal_user('[email protected]')
3207
        uapi.save(user2)
3208
3209
        RoleApi(
3210
            current_user=user1,
3211
            session=self.session,
3212
            config=self.app_config,
3213
        ).create_one(
3214
            user2,
3215
            workspace,
3216
            UserRoleInWorkspace.CONTENT_MANAGER,
3217
            with_notif=True,
3218
            flush=True
3219
        )
3220
3221
        # show archived is used at the top end of the test
3222
        api = ContentApi(
3223
            current_user=user1,
3224
            session=self.session,
3225
            config=self.app_config,
3226
            show_deleted=True,
3227
        )
3228
        p = api.create(
3229
            content_type_slug=content_type_list.File.slug,
3230
            workspace=workspace,
3231
            parent=None,
3232
            label='this_is_a_page',
3233
            do_save=True
3234
        )
3235
3236
        u1id = user1.user_id
3237
        u2id = user2.user_id
3238
        pcid = p.content_id
3239
        poid = p.owner_id
3240
3241
        transaction.commit()
3242
3243
        ####
3244
        user1 = UserApi(
3245
            current_user=None,
3246
            session=self.session,
3247
            config=self.app_config,
3248
        ).get_one(u1id)
3249
        workspace = WorkspaceApi(
3250
            current_user=user1,
3251
            session=self.session,
3252
            config=self.app_config,
3253
        ).get_one(wid)
3254
3255
        content = api.get_one(pcid, content_type_list.Any_SLUG, workspace)
3256
        eq_(u1id, content.owner_id)
3257
        eq_(poid, content.owner_id)
3258
3259
        u2 = UserApi(
3260
            current_user=None,
3261
            session=self.session,
3262
            config=self.app_config,
3263
        ).get_one(u2id)
3264
        api2 = ContentApi(
3265
            current_user=u2,
3266
            session=self.session,
3267
            config=self.app_config,
3268
            show_deleted=True,
3269
        )
3270
        content2 = api2.get_one(pcid, content_type_list.Any_SLUG, workspace)
3271
        with new_revision(
3272
                session=self.session,
3273
                tm=transaction.manager,
3274
                content=content2,
3275
        ):
3276
            api2.delete(content2)
3277
        api2.save(content2)
3278
        transaction.commit()
3279
3280
        ####
3281
3282
        user1 = UserApi(
3283
            current_user=None,
3284
            session=self.session,
3285
            config=self.app_config,
3286
        ).get_one(u1id)
3287
        workspace = WorkspaceApi(
3288
            current_user=user1,
3289
            session=self.session,
3290
            config=self.app_config,
3291
        ).get_one(wid)
3292
        # show archived is used at the top end of the test
3293
        api = ContentApi(
3294
            current_user=user1,
3295
            session=self.session,
3296
            config=self.app_config,
3297
            show_deleted=True,
3298
        )
3299
        u2 = UserApi(
3300
            current_user=None,
3301
            session=self.session,
3302
            config=self.app_config,
3303
        ).get_one(u2id)
3304
        api2 = ContentApi(
3305
            current_user=u2,
3306
            session=self.session,
3307
            config=self.app_config,
3308
            show_deleted=True
3309
        )
3310
3311
        updated = api2.get_one(pcid, content_type_list.Any_SLUG, workspace)
3312
        eq_(u2id, updated.owner_id,
3313
            'the owner id should be {} (found {})'.format(u2id,
3314
                                                          updated.owner_id))
3315
        eq_(True, updated.is_deleted)
3316
        eq_(ActionDescription.DELETION, updated.revision_type)
3317
3318
        ####
3319
3320
        updated2 = api.get_one(pcid, content_type_list.Any_SLUG, workspace)
3321
        with new_revision(
3322
            tm=transaction.manager,
3323
            session=self.session,
3324
            content=updated2,
3325
        ):
3326
            api.undelete(updated2)
3327
        api.save(updated2)
3328
        eq_(False, updated2.is_deleted)
3329
        eq_(ActionDescription.UNDELETION, updated2.revision_type)
3330
        eq_(u1id, updated2.owner_id)
3331
3332
    def test_unit__get_last_active__ok__nominal_case(self):
3333
        uapi = UserApi(