diff --git a/tests/cypress/integration/admin.test.js b/tests/cypress/integration/admin.test.js index 51b3d7c53..ce71808d4 100644 --- a/tests/cypress/integration/admin.test.js +++ b/tests/cypress/integration/admin.test.js @@ -48,13 +48,20 @@ describe( 'Admin can login and make sure plugin is activated', () => { .contains( 'Image Processing' ); } ); - it( 'Can visit the general settings page and see all settings.', () => { + it( 'Can see "Usage Tracking" menu and Can visit "AI Usage Tracking" settings page.', () => { // Check Selected Navigation menu - cy.visitFeatureSettings( 'settings' ); + cy.visitFeatureSettings( 'usage_tracking/api_usage_tracking' ); cy.get( '.classifai-tabs' ).should( 'exist' ); cy.get( '.classifai-tabs a.active-tab' ) .first() - .contains( 'Settings' ); + .contains( 'Usage Tracking' ); + } ); + + it( 'Can visit the general settings page and see all settings.', () => { + // Check Selected Navigation menu + cy.visitFeatureSettings( 'settings' ); + cy.get( '.classifai-tabs' ).should( 'exist' ); + cy.get( '.classifai-tabs a.active-tab' ).first().contains( 'Settings' ); // Check that all settings are present. cy.get( '.components-input-control input[type="email"]' ).should( diff --git a/tests/cypress/integration/admin/common-feature-fields.test.js b/tests/cypress/integration/admin/common-feature-fields.test.js index 5afa7f16e..3c6614605 100644 --- a/tests/cypress/integration/admin/common-feature-fields.test.js +++ b/tests/cypress/integration/admin/common-feature-fields.test.js @@ -24,6 +24,9 @@ describe( 'Common Feature Fields', () => { content_recommendation: { feature_recommended_content: 'Recommended Content', }, + usage_tracking: { + api_usage_tracking: 'API Usage Tracking', + }, }; const allowedRoles = [ 'administrator', 'editor', 'author', 'contributor' ]; @@ -64,7 +67,15 @@ describe( 'Common Feature Fields', () => { `.settings-allowed-roles input#${ role }` ); roleField.should( 'be.visible' ); - roleField.should( 'have.value', 1 ); + + if ( + 'api_usage_tracking' === feature && + 'administrator' !== role + ) { + roleField.should( 'not.be.checked' ); + } else { + roleField.should( 'be.checked' ); + } } cy.get( '.classifai-settings__users' ).should( 'be.visible' ); diff --git a/tests/cypress/integration/usage-tracking/api-usage-tracking-openai.test.js b/tests/cypress/integration/usage-tracking/api-usage-tracking-openai.test.js new file mode 100644 index 000000000..911df2253 --- /dev/null +++ b/tests/cypress/integration/usage-tracking/api-usage-tracking-openai.test.js @@ -0,0 +1,39 @@ +describe( '[Usage Tracking] API Usage Tracking (OpenAI) Tests', () => { + before( () => { + cy.login(); + cy.optInAllFeatures(); + cy.disableClassicEditor(); + } ); + + beforeEach( () => { + cy.login(); + } ); + + it( 'Can save AI Usage Tracking settings', () => { + cy.visitFeatureSettings( 'usage_tracking/api_usage_tracking' ); + + cy.enableFeature(); + cy.selectProvider( 'openai_usage_tracking' ); + cy.get( '#openai_usage_tracking_api_key' ).clear().type( 'sk-admin-password' ); + cy.get( '#openai_usage_tracking_project_id' ).clear().type( 'proj_cypress_openai_1' ); + + cy.allowFeatureToAdmin(); + cy.saveFeatureSettings(); + } ); + + it( 'Can see "AI Usage Tracking" Widget on WP Dashboard', () => { + cy.visit( `/wp-admin/index.php` ); + + const aiUsageWidget = cy.get( '#dashboard-widgets #classifai_api_usage' ); + + aiUsageWidget.should( 'exist' ); + aiUsageWidget.get( 'h2' ).should( 'contain', 'AI Usage Tracking' ); + aiUsageWidget.get( '.classifai-api-usage-list li:first-child' ).should( 'contain', 'This month:' ); + aiUsageWidget.get( '.classifai-api-usage-list li:first-child' ).should( 'contain', 'Updating…' ); + aiUsageWidget.get( '.classifai-api-usage-list li:nth-child(2)' ).should( 'contain', 'Year to date:' ); + aiUsageWidget.get( '.classifai-api-usage-list li:nth-child(2)' ).should( 'contain', 'Updating…' ); + aiUsageWidget.get( '.classifai-api-usage-list li:nth-child(3)' ).should( 'contain', 'All time:' ); + aiUsageWidget.get( '.classifai-api-usage-list li:nth-child(3)' ).should( 'contain', 'Updating…' ); + aiUsageWidget.get( '.classifai-api-usage-updated' ).should( 'not.exist' ); + } ); +} ); diff --git a/tests/test-plugin/e2e-test-plugin.php b/tests/test-plugin/e2e-test-plugin.php index ae675909a..7e0a2194d 100644 --- a/tests/test-plugin/e2e-test-plugin.php +++ b/tests/test-plugin/e2e-test-plugin.php @@ -154,6 +154,8 @@ function classifai_test_mock_http_requests( $preempt, $parsed_args, $url ) { $response = file_get_contents( __DIR__ . '/stable-diffusion-models.json' ); } elseif ( strpos( $url, 'http://127.0.0.1:7860/sdapi/v1/txt2img' ) !== false ) { $response = file_get_contents( __DIR__ . '/stable-diffusion.json' ); + } elseif ( strpos( $url, 'https://api.openai.com/v1/organization/admin_api_keys' ) !== false ) { + $response = file_get_contents( __DIR__ . '/mock-data/openai-admin-api-keys.json' ); } if ( ! empty( $response ) ) { diff --git a/tests/test-plugin/mock-data/openai-admin-api-keys.json b/tests/test-plugin/mock-data/openai-admin-api-keys.json new file mode 100644 index 000000000..0ea77dc64 --- /dev/null +++ b/tests/test-plugin/mock-data/openai-admin-api-keys.json @@ -0,0 +1,40 @@ +{ + "object": "list", + "data": [ + { + "object": "organization.admin_api_key", + "id": "key_iMsVywuMiZ8QHsBb", + "name": "ClassifAI Test Admin API Key", + "redacted_value": "sk-proj-********************************************************************************************************************************************************ilkA", + "created_at": 1738616855, + "last_used_at": 1773926235, + "owner": { + "type": "user", + "object": "organization.user", + "id": "user-y8Dd7nRo6DLw97YibFGmgifw", + "name": "ClassifAI", + "created_at": 1738614303, + "role": "reader" + } + }, + { + "object": "organization.admin_api_key", + "id": "key_vL7kHhxaNHqZLCE4", + "name": null, + "redacted_value": "sk-proj-********************************************************************************************************************************************************vaIA", + "created_at": 1743612682, + "last_used_at": 1770150101, + "owner": { + "type": "user", + "object": "organization.user", + "id": "user-mnH7BTMVDTAzXcR8xroiENJT", + "name": "ClassifAI User 1", + "created_at": 1643126205, + "role": "owner" + } + } + ], + "first_id": "key_iMsVywuMiZ8QHsBb", + "last_id": "key_vL7kHhxaNHqZLCE4", + "has_more": false +}